James Kipp wrote:
>
> I want to split the string below into rows and then columns. I know i can do
> a couple of splits, push array refs onto a new array, regex it, etc.. But I
> was know the talented people on this list can shorten the code considerably.
> I want to split at each new line for the row, then split the row into
> columns, and get rid of the "%" on the third column. I can post my
> embarrassingly long code if you like.
> Thanks for any help
> Jim
> ----
>
> $str = "
> /var                      0.99    50%
> /usr                      0.58    71%
> /tmp                      0.49     1%
> "

Hi James.

Don't forget that shortening code is usually more of a pastime
than anything useful, but if your code is /huge/ it can add to
readability.

Does this help? The grep just throws out blank lines.

Cheers,

Rob


  use strict;
  use warnings;

  my $str = "
  /var                      0.99    50%
  /usr                      0.58    71%
  /tmp                      0.49     1%
  ";

  my @data = map { tr/%//d; [split ' '] } grep /\S/, split "\n", $str;

  use Data::Dumper;
  print Dumper [EMAIL PROTECTED];

**OUTPUT

$VAR1 = [
          [
            '/var',
            '0.99',
            '50'
          ],
          [
            '/usr',
            '0.58',
            '71'
          ],
          [
            '/tmp',
            '0.49',
            '1'
          ]
        ];




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