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

Hi, I'm new at perl and don't really come from a programming background - I'm trying to read a file which contains 3 columns of data(numbers) on each line that I've read into an array. How do I assign variable(scalar) values to each element on each line so as to read what's in them? Or are there other/easier ways to read each element not incorporating an array? ex. open(FhIn, "data.d"); @lines=<FhIn>; Any help would be greatly appreciated. Thanks!
  • Comment on array - separating elements in a 3-column list

Replies are listed 'Best First'.
Re: array - separating elements in a 3-column list
by Zaxo (Archbishop) on Jan 27, 2005 at 02:30 UTC

    Split will seperate $_ on whitespace if given no arguments,

    #!/usr/bin/perl # file: foo.pl # Usage: ./foo.pl /path/to/data.d use warnings; use strict; while (<>) { my ($foo, $bar, $baz) = split; # do stuff with them } # report results
    The empty spaceship op takes care of opening and reading files from @ARGV. If no files are given, it will read from STDIN until Ctrl-D or eof.

    After Compline,
    Zaxo

Re: array - separating elements in a 3-column list
by lidden (Curate) on Jan 27, 2005 at 02:38 UTC
    Maybe somthing liket this:
    use strict; use warnings; my $file = 'data.d'; my $FhIn; open $FhIn, '<', $file or die "Could not open $file: $!"; while( my $line = <$FhIn> ){ my ($first, $second, $third) = split ' ', $line; # do something # print "$first : $second : $third\n"; } close $FhIn or die $!;
Re: array - separating elements in a 3-column list
by K_M_McMahon (Hermit) on Jan 27, 2005 at 04:46 UTC
    Depending on what you want to do with it once you have it in the script, you could put the elements into 3 different hashes, one for the left, one for the middle and one for the right. This would allow you to reference all the elements at once or seperately later on.
    use strict; use warnings; my $file = '3column.file'; my $i=0; open (INPUT, "<$file") or die "Could not open $file: $!"; my (%left_hash,%middle_hash,%right_hash); while (<INPUT>) { my ($left, $middle, $right) = split (/\s+/, $_); $left_hash{$i}=$left; $middle_hash{$i}=$middle; $right_hash{$i}=$right; $i++; }
      Or you could use an array of hashes.
      #hoaMessing.pl use strict; use warnings; use Data::Dumper; my $file = '3column.file'; #contains #a b c #d e f #g h i my $i=0; open (INPUT, "<$file") or die "Could not open $file: $!"; my (%left_hash,%middle_hash,%right_hash); my $line_number = 0; my @lines; while (<INPUT>) { my %line; ($line{left},$line{middle},$line{right}) = split (/\s+/, $_); $line{line_number} = $line_number; push @lines,\%line; $line_number++; } print Dumper(\@lines); #prints #$VAR1 = [ # { # 'middle' => 'b', # 'left' => 'a', # 'right' => 'c', # 'line_number' => 0 # }, # { # 'middle' => 'e', # 'left' => 'd', # 'right' => 'f', # 'line_number' => 1 # }, # { # 'middle' => 'h', # 'left' => 'g', # 'right' => 'i', # 'line_number' => 2 # } # ];