Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

When I run this code:

open(FILE, "data.txt"); #opens data.txt in read-mode while(<FILE>){ #reads line by line from FILE which i +s the filehandle for data.txt chomp; @data = split(/,/,$_); } close FILE; #close the file. print "test:\t$_\n" foreach (@data);
it only displays the last row in the data.txt file. How can I get it to ready each line and create an array for each line? How would a has be used here?

Replies are listed 'Best First'.
Re: problem with array
by Hot Pastrami (Monk) on Mar 14, 2001 at 04:22 UTC
    Your code reassigns the entire contents of the array on each loop... if you want to collect it all inone large array, try this instead:
    while(<FILE>){ chomp; push @data, split(/,/,$_); }
    ...If you want individual arrays, you'll have to get fancier:
    my @data = (); while(<FILE>){ chomp; push @data, [ split(/,/,$_) ]; }
    This will make an array of arrays, accessible as such:
    print data[0][0]; # prints the first element of the first line print data[4][3]; # prints the fifth element of the fourth line


    Hot Pastrami
Re: problem with array
by lhoward (Vicar) on Mar 14, 2001 at 04:24 UTC
    open(FILE, "data.txt") or die "$!"; while(<FILE>){ chomp; my @data = split(/,/,$_); print "test:\t$_\n" foreach (@data); } close FILE;
    or
    my @lines; open(FILE, "data.txt") or die "$!"; while(<FILE>){ chomp; my @data = split(/,/,$_); push @lines,\@data; } close FILE; foreach(@lines){ print "test:\t$_\n" foreach (@$_); }
    depending on whether you want to store all of the lines in the file in an array for later processing....
Re: problem with array
by mkmcconn (Chaplain) on Mar 14, 2001 at 06:31 UTC
    I'm guessing, but I think you asked about how a hash would be used, which makes me think that what you are asking for might be something like this:
    open FILE, "data.txt" or die $!; while(<FILE>){ chomp; my ($uniq,$one,$two,) = split ','; # split the line into its fields $line_hash{$uniq} = qq($uniq $one $two); # use a _unique_ key value # describing the line to define # a hash key, and splice the # line to that key. } close FILE; # now sort by the (presumed) # numerical key, and print a test. for( sort { $a <=> $b } keys %line_hash ){ print "test: ",$line_hash{$_},$/ ; }

    The test will print out the first three comma-separated fields of each line, sorted by the key. Combine that, with what was posted by Hot Pastrami about creating and accessing anonymous arrays, or what lhoward posted about references to arrays, and I think you have a complete answer.

    mkmcconn

Re: problem with array
by jeroenes (Priest) on Mar 14, 2001 at 17:57 UTC
    Aha! Here is another one for Supersplit! D/l the module, and use it like:
    use SuperSplit; $array=supersplit_open('/,/',"data.txt"); print superjoin("\t",$array);
    Good luck with it,

    Jeroen
    "We are not alone"(FZ)

      open(FILE, "data.txt"); #opens data.txt in read-mode while(<FILE>){ #reads line by line from FILE which i +s the filehandle for data.txt chomp; push(@data,split(/,/,$_)); } close FILE; #close the file. print "$_\n" foreach (@data);



      Now that I've read the comma delimited rows, split the values, and printed them out, I'd like to only get the middle value and assign it as $co. Basically, how do I just pull the first, second, or third value based on my preference?

        Hmmmm... either I don't understand your question or you'd better have a good ponder at them docs. Do you mean:
        push @data, [split /,/]->[$col] for (<FILE>);
        ?

        Jeroen
        "We are not alone"(FZ)

Re: problem with array
by AgentM (Curate) on Mar 14, 2001 at 04:24 UTC
    You're obviously a beginner that needs to read some more documentation. This site has somewhat current documentation starting at perlman and Tutorials. The reason you are only getting the last line is because you assign the split line to the array which overwrites any previous value. Instead, use push as in
    push(@data,$_);
    That way, you store all of the values along the way. Perhaps you could split later, too, otherwise you would need to learn nested/referenced data structures.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.