> I know this is simpler then what I am making it but I am stumped.  I used
LWP::UserAgent to fetch some data from a
> web page, What I need to do is to load the data which I believe is just
one long string that I got from calling content()
> into an array of arrays by splitting on "\n" and ",".
[snip]
>     sub quotes{
>          my $content = get_content();
>          my (@data,$i);
>          my @rows=split/\n/,$content;
>          $i=0;
>          foreach my $element (@rows){
>               $data[$i]=split(/,/,$element);

Your problem is here.  split returns an array, but you are assigning it to a
scalar variable.  The array is interpreted in scalar context, i.e. setting
the scalar to the length of the array.  What you want to do is assign an
array reference, e.g. an anonymous array containing the output of split like
so:

$data[$i] = [ split( /,/, $element ) ];

and then carry on
>               $i++;
>           }
>           return @data;
>      }

As a PS I've included a working script and its output.  If you have a copy
of 'Programming Perl' (3rd edition) read Chapter 8 and 9 (References and
Data Structures).  It took me several read-throughs before it all sunk in
(and I'm sure I still don't fully understand it) but it's worth it.

Best wishes,

Rachel

P.S.
#!/usr/bin/perl -w
use strict;

my $test_string = "lots of text,with,commas\nand\nnewlines,here there and \n
everywhere, just for fun\n you\n see";

my @rows = split( /\n/, $test_string );
my $i=0;
my @data;
foreach my $element ( @rows ) {
    $data[$i] = [ split( /,/, $element ) ];
    $i++;
}

foreach ( @data ) {
    foreach ( @$_ ) {
        print "$_\n";
    }
}
print "Done!\n";

# Output is
lots of text
with
commas
and
newlines
here there and
 everywhere
 just for fun
 you
 see
Done!



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to