pushing a string into a array

pushing a string into a array

am 14.05.2011 09:57:01 von Agnello George

hi

I got a string like this

$string = ' [a b c d]'

i need to get a b c d in to a array called @all.

i was was trying to so a split with delimiter '\s+' but still i get

[a
b
c
d]

but i want

a
b
c
d


any idea how to get this done , thanks
--
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: pushing a string into a array

am 14.05.2011 10:07:11 von Brandon McCaig

On Sat, May 14, 2011 at 3:57 AM, Agnello George
wrote:
> I got a string like this
>
> $string  =3D ' [a b  c  d]'
>
> i need to get a b c d in to a array called @all.
>
> i was was trying to so a split with delimiter '\s+' but still  i get
>
> [a
> b
> c
> d]
>
> but i want
>
> a
> b
> c
> d
>
>
> any idea how to get this done , thanks

TIMTOWTDI. There is more than one way to do it. The question first
becomes how specific is the string. Do you only want to know how to do
it with your example string or is the actual string somewhat variable?
For example:

use strict;
use warnings;

use Data::Dumper;

my $string =3D '[a b c d]';

my @results =3D $string =3D~ /(\w)/g;

print Dumper \@results;

Match a single word-character and capture it; do this globally
throughout the string, and store all of the captured results in an
array.

--=20
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software ..org>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: pushing a string into a array

am 14.05.2011 10:42:53 von Agnello George

On Sat, May 14, 2011 at 1:37 PM, Brandon McCaig wrote:
> On Sat, May 14, 2011 at 3:57 AM, Agnello George
> wrote:
>> I got a string like this
>>
>> $string  = ' [a b =A0c =A0d]'
>>
>> i need to get a b c d in to a array called @all.
>>
>> i was was trying to so a split with delimiter '\s+' but still =A0i get
>>
>> [a
>> b
>> c
>> d]
>>
>> but i want
>>
>> a
>> b
>> c
>> d
>>
>>
>> any idea how to get this done , thanks
>
> TIMTOWTDI. There is more than one way to do it. The question first
> becomes how specific is the string. Do you only want to know how to do
> it with your example string or is the actual string somewhat variable?
> For example:
>
> use strict;
> use warnings;
>
> use Data::Dumper;
>
> my $string =3D '[a b c d]';
>
> my @results =3D $string =3D~ /(\w)/g;
>
> print Dumper \@results;
>
> Match a single word-character and capture it; do this globally
> throughout the string, and store all of the captured results in an
> array.
>
> --
> Brandon McCaig
> V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
> Castopulence Software ce.org>
>

thanks that worked !!

--=20
Regards
Agnello D'souza

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: pushing a string into a array

am 14.05.2011 10:45:26 von Rob Dixon

On 14/05/2011 08:57, Agnello George wrote:
>
> I got a string like this
>
> $string = ' [a b c d]'
>
> i need to get a b c d in to a array called @all.
>
> i was was trying to so a split with delimiter '\s+' but still i get
>
> [a
> b
> c
> d]
>
> but i want
>
> a
> b
> c
> d
>
>
> any idea how to get this done , thanks

By the way, a call to split with a single space string as the pattern is
a special case. It is the default if no pattern is provided, and more
than likely what you want. This is from perldoc -f split

> As a special case, specifying a PATTERN of space (' ') will split on
> white space just as "split" with no arguments does. [...] A "split"
> on "/\s+/" is like a "split(' ')" except that any leading whitespace
> produces a null first field. A "split" with no arguments really does
> a "split(' ', $_)" internally.

I am sure the problem is more complex han you describe, but to be safe
why not just remove the brackets before you do the split? A working
program below.

HTH,

Rob


use strict;
use warnings;

my $string = ' [a b c d]';

$string =~ tr/[]//d;
my @contents = split ' ', $string;

print "$_\n" foreach @contents;

**OUTPUT**

a
b
c
d

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/