split & re
am 15.05.2011 18:28:36 von Mike McClain
In reading in a file of space separated columns of numbers
and stuffing them into an array, I used:
while( my $line = <$FH> )
{ my @arr = split /\s+/, $line;
push @primes_array, @arr;
}
but kept getting empty array entries until I switched to:
while( <$FH> )
{ my @arr = split;
push @primes_array, @arr;
}
perldoc -f split says:
If EXPR is omitted, splits the $_ string.
If PATTERN is also omitted, splits on whitespace
(after skipping any leading whitespace).
My question is what RE do I need to pass to split to get
the same return from split /$RE/, $line;
as I get from the split against $_.
Thanks,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: split & re
am 15.05.2011 20:51:35 von jwkrahn
Mike McClain wrote:
> In reading in a file of space separated columns of numbers
> and stuffing them into an array, I used:
> while( my $line =<$FH> )
> { my @arr = split /\s+/, $line;
> push @primes_array, @arr;
> }
>
> but kept getting empty array entries until I switched to:
> while(<$FH> )
> { my @arr = split;
> push @primes_array, @arr;
> }
>
> perldoc -f split says:
> If EXPR is omitted, splits the $_ string.
> If PATTERN is also omitted, splits on whitespace
> (after skipping any leading whitespace).
>
> My question is what RE do I need to pass to split to get
> the same return from split /$RE/, $line;
> as I get from the split against $_.
perldoc -f split
As a special case, specifying a PATTERN of space (' ') will
split on white space just as "split" with no arguments does
. Thus, "split(' ')" can be used to emulate awk's default
behavior, whereas "split(/ /)" will give you as many null
initial fields as there are leading spaces. 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.
So you want:
split ' ', $line;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: split & re
am 15.05.2011 20:57:32 von charley
On May 15, 12:28=A0pm, mike.j...@cox.net (Mike McClain) wrote:
> In reading in a file of space separated columns of numbers
> and stuffing them into an array, I used:
> =A0 =A0 while( my $line =3D <$FH> )
> =A0 =A0 { =A0 my @arr =3D split /\s+/, $line;
> =A0 =A0 =A0 =A0 push @primes_array, @arr;
> =A0 =A0 }
>
> but kept getting empty array entries until I switched to:
> =A0 =A0 while( <$FH> )
> =A0 =A0 { =A0 my @arr =3D split;
> =A0 =A0 =A0 =A0 push @primes_array, @arr;
> =A0 =A0 }
>
> perldoc -f split says:
> If EXPR is omitted, splits the $_ string. =A0
> If PATTERN is also omitted, splits on whitespace
> (after skipping any leading whitespace).
>
> My question is what RE do I need to pass to split to get
> the same return from =A0 =A0split /$RE/, $line;
> as I get from the split against $_.
>
> Thanks,
> Mike
> --
> Satisfied user of Linux since 1997.
> O< ascii ribbon campaign - stop html mail -www.asciiribbon.org
Hello Mike
Boy, you were close. ;-)
Also from documentation on split:
As a special case, specifying a PATTERN of space (' ' ) will split on
white space just as split with no arguments does. Thus, split(' ') can
be used to emulate awk's default behavior, whereas split(/ /) will
give you as many initial null fields (empty string) as there are
leading spaces. 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.
Chris
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: split & re
am 15.05.2011 22:26:12 von rvtol+usenet
On 2011-05-15 18:28, Mike McClain wrote:
> In reading in a file of space separated columns of numbers
> and stuffing them into an array, I used:
> while( my $line =<$FH> )
> { my @arr = split /\s+/, $line;
> push @primes_array, @arr;
> }
>
> but kept getting empty array entries until I switched to:
> while(<$FH> )
> { my @arr = split;
> push @primes_array, @arr;
> }
>
> perldoc -f split says:
> If EXPR is omitted, splits the $_ string.
> If PATTERN is also omitted, splits on whitespace
> (after skipping any leading whitespace).
>
> My question is what RE do I need to pass to split to get
> the same return from split /$RE/, $line;
> as I get from the split against $_.
That can be your question, but have you also compared the lines with the
differences? They probably start with whitespace.
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: split & re
am 17.05.2011 01:45:56 von Mike McClain
On Sun, May 15, 2011 at 11:51:35AM -0700, John W. Krahn wrote:
>
> split ' ', $line;
>
> John
Smacking my forehead in chagrin. :-)
You're absolutely right John, I just didn't read far enough.
Thank you,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/