> From: Jenda Krynicky [mailto:[EMAIL PROTECTED] 
> Sent: Saturday, 21 February 2004 10:01 AM
> To: Perl Beginners
> Subject: Re: list of strings to array
> 
> From: Jacob Chapa <[EMAIL PROTECTED]>
> > Is there any way to split up a string and put it into an array....
> > 
> > something like this:
> > 
> > if I had a string:
> > 
> > one two three four
> > 
> > or
> > 
> > one;two;three;four
> > 
> > Can I put "one" into the first element, "two" into the 
> second, etc...
> > knowing what splits up the string. For instance, in the first one a
> > space " " separates it and in the second a semicolon ";" 
> separates the
> > elements.
> 
> Guess what ... 
> 
>       @array = split / /, "one two three four";
>       @array2 = split /;/, "one;two;three;four";
> 

Just a note.

@array = split / /, "one two three four";

and

@array = split " ", "one two three four";

are not the same. The second is special-cased inside perl to do the
same as the AWK split command.  The first is just a normal regular
expression matching exactly one space.

Hence

if
        $a=" one two  three four ";  (note the leading and extra spaces)

after 
        @array = split / /, $a

@array will contain ( undef, 'one', 'two', undef, 'three', 'four', undef
)

ie, seven (7) elements from a string with six (6) separators.

while after 
        @array = split " ", $a

@array will contain ( 'one', 'two', 'three', 'four' )

which is more intuitive.

Cheers.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to