On 03/08/2011 20:30, Rob Dixon wrote:

Because the values of the array elements are simple strings, as I
explained above, you must find a way of splitting them into
individual fields. The simple way would be to use the 'split' operator
to divide the line at whitespace, but the presence of spaces in the
description field spoils that approach. Without knowing more about your
data, I can suggest that you split on sequences of two or more
whitespace characters, like this

   my @cart_lineitems;
   foreach my $i (keys  %{$global->{cart}}) {
     $cart_lineitems[$i] = [ split /\s{2,}/, $global->{cart}{$i} ];
   }

which dumps like this

@cart_lineitems = (
           undef,
           [
             '1 1 SL-8206',
             '73.15',
             'Label 8.25" x 6"',
             '0.8 73.15 '
           ],
           [
             '2 1 APT-46V',
             '96.43',
             '4"x6" Label, hook',
             '1.8 96.43 '
           ],
           [
             '3 1 WR-9253',
             '13.98',
             'Label - 3" x 9"',
             '0.5 13.98 '
           ]
         );

Which is reasonable, but you still have trailing spaces at the end of
the final field, which may or may not be a problem.

Marc, I have since noticed that your original hash values contain tab
characters to separate the fields. THis makes things a lot easier, and
you can write

  my @cart_lineitems = map [ split /\t+/ ], values %{$global->{cart}};

which now looks like like the dump below.

Cheers,

Rob


@cart_lineitems = (
                    [
                      '1',
                      '1',
                      'SL-8206',
                      '73.15',
                      'Label 8.25" x 6"',
                      '0.8',
                      '73.15'
                    ],
                    [
                      '3',
                      '1',
                      'WR-9253',
                      '13.98',
                      'Label - 3" x 9" ',
                      '0.5',
                      '13.98'
                    ],
                    [
                      '2',
                      '1',
                      'APT-46V',
                      '96.43',
                      '4"x6" Label, hook ',
                      '1.8',
                      '96.43'
                    ]
                  );

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to