in reply to Reading arrays

Well, this should do the trick. I did not do any bounds error checking on the arrays that hold the files. This will just give you an idea how to solve the problem:
use strict; my (@lines1, @lines2, @lines3); &load_array(\@lines1, "foo1.dat"); &load_array(\@lines2, "foo2.dat"); &load_array(\@lines3, "foo3.dat"); for (my $i = 0; $i < @lines1; $i++) { my ($word2) = split(/ /, $lines2[$i]); my ($word3) = split(/ /, $lines3[$i]); print "$lines1[$i]:$word2:$word3\n"; } sub load_array($$) { my ($a_ref, $file) = @_; open (FILE, $file) or die "Can't open $file:$!\n"; @{$a_ref} = <FILE>; close FILE; chomp @{$a_ref}; }
As you can see, this will bomb if the first file contains more lines than the other two files - recursive turnary operator, anyone? There is lots of improving that can be done to this rather unelegant piece of code . . . but this does the trick - hope this helps.

Replies are listed 'Best First'.
RE: Re: Reading arrays
by Anonymous Monk on Jul 31, 2000 at 22:34 UTC
    Thankx.I wanted to get certain lines rather than all lines. What should I use rather than for loop?
      Just change the entire for loop to this:
      foreach (qw(0 3 8 20)) { my ($word2) = split(/ /, $lines2[$_]); my ($word3) = split(/ /, $lines3[$_]); print "$lines1[$_]:$word2:$word3\n"; }
      Just substitute the desired line numbers (each one -1, remember) inside qw(). If you do have large data files, use gryng's method below.

      To grying: sorry for making you gag, I am studying your method closely to make up for it.