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

i have a file that has a huge numbers of lines and each line contains 5 numbers 1st is an x value and the following 4 are y values. i want to read each value into a different array so that by the end of the file i will have an array of x values and 4 arrays of y values.

i was told by another source that i could use a foreach loop to state that i wanted it to go through each line and then get an array of each line with a split(/ /) command the only problem is that i have no idea how to get a foreach loop to go line by line.

i dont need it to store these values for any longer than it takes for it to find a max and a min for the x and a max for each y

all numbers are in a 0.00000000E00 config and there are negatives present.

what would be the most graceful way that anyone can think of to get through this.

any suggestion would be much appreciated.

Restored paragraph div's - dvergin 2002-07-03

Replies are listed 'Best First'.
Re: n00b reading info from file
by Popcorn Dave (Abbot) on Jul 03, 2002 at 18:09 UTC
    You'll probably get a few other examples but this is one way to do it:

    p.s. This does assume you're splitting on spaces in your data file. If not, change the value in split to whatever is seperating your data.

    #!/usr/bin/perl -w use strict; my @x; # your x values my @y; # your y values open FH, 'your file'; # whatever your data file is while (my $line = <FH>){ my @z = split ' ', $line; # split on space to get # your values in an array push(@x ,unshift @z); # take first element of z and # add it to x push(@y,$_) foreach(@z); # add the rest to y } close FH;

    That may not be the cleanest way but to do what you want to do, the push and pop do work. After you have your values seperated, it should be trivial for you to work out the max values for each.

    Good luck!

    Some people fall from grace. I prefer a running start...

    Update:

    Aristotle was kind enough to point out I had misread the question slightly. Below is an updated version that's about as easy as I can do.

    #!/usr/bin/perl -w use strict; my @x; # your x values my @y1; # your y values my @y2; my @y3; my @y4; die unless open FH, 'file.txt'; # whatever your data file # is my $count=0; while (my $line = <FH>){ ($x[$count], $y1[$count], $y2[$count], $y3[$count], $y4[$count]) = split ' ', $line; # split on space to get # your values in an array $count++; } close FH; print "X:@x\n1:@y1\n2:@y2\n3:@y3\n4:@y4\n";

    Please ignore the top code unless you need to get a list of numbers in to two arrays with the first array holding the first number and the second the rest of the numbers for your file. : )

      Something you may want to pay attention to: you didn't check your open for success. Make it a habit:

      open FH, "<$filename" or die "can't open $filename: $!"; Also, skip the open business entirely (and process as many files as you want or if none given optionally take input from STDIN) by simply using <> to read: while (my $line = <>){ Lastly.. note that the author wanted the four y values in separate arrays..

      Makeshifts last the longest.

        Ah, you're right about the number of arrays. I guess I just read it wrong.

        As far as the solution, I agree, it's wise to check for open success. I just sort of did that on the fly to nudge the person towards their goal.

        Some people fall from grace. I prefer a running start...

Re: n00b reading info from file
by Aristotle (Chancellor) on Jul 03, 2002 at 19:56 UTC

    You can do two things: either the approach you already mentioned, using five arrays, and maybe that's what you really want. Alternatively however, an array of arrays would probably a be better solution.

    Using five arrays:
    #!/usr/bin/perl -w use strict; my (@x,@y1,@y2,@y3,@y4); while(<>) { # reads a line into $_ chomp; # remove the newline from its end my @val = split; # with no parameters, split() splits $_ on whites +pace push @x, # push() to append to the end of @x shift @val; # shift pulls the first element out of an array push @y1, shift @val; push @y2, shift @val; push @y3, shift @val; push @y4, shift @val; }
    But, probably much better, using an array of arrays:
    #!/usr/bin/perl -w use strict; my @val; while(<>) { chomp; push @val, [ split ]; }
    or even chomp, push @val, [ split ] while <>; as the last part. Then you can later access the elements with something like print $val[3]->[0]; to see the x value of the 4th line. To see how it all works, have a look into the perllol (lol = list of lists) and the perlreftut documentation.

    Makeshifts last the longest.