--- 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];

That prints:

  $VAR1 = [
            '50',
            '100',
            '150',
            '200',
            '300',
            '50'
          ];

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

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


Reply via email to