Ovid wrote:
> --- Bram Kuijper <[EMAIL PROTECTED]> wrote:
>
>> I have a single line of whitespace separated values, e.g.:
>>
>>                    50                     100
>> 150             200               300             50
>>
>> Now I want to split these values into an array using split, like:
>>
>> my @array = split(/\s+/,$line);
>>
>> but, unfortunately, the first value in the array is now an empty
>> value.
>
> A little known feature of 'split' is that if you call split without
> arguments, it automatically splits on whitespace in $_, trimming
> extraneous whitespace.
>
>   #!/usr/bin/perl
>
>   use strict;
>   use warnings;
>   use Data::Dumper;
>
>   my $line = '                   50                     100
>   150             200               300             50';
>
>   my @array = do { local $_ = $line; split };
>   print Dumper [EMAIL PROTECTED];

[snip]

The default behaviour can be invoked explicitly by using a single space as the
separator parameter for split. So

my @array = split ' ', $line;

has the desired effect.

Note that this must be a single-space /string/, not a regular expression.

HTH,

Rob

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


Reply via email to