Read files, lines into array, cat vs open

Hi Everyone,

I have a file: a.txt
a,b,c,d,6,6,6
1,2,3,d,6,6,6
4,5,6,6,6,6,6

#!/usr/bin/perl
use warnings;
use strict;
my @array = ();
### Load file into array
for my $i (split '\n', `cat /tmp/a.txt`) {
    push @array, [ split ',', $i ];
}

It works. But my a.txt have 1million lines, and each lines have 17 fields. If use 'cat' like above, it takes veeeeeeeeeery long time.

How to do it using open?, especially need to split ',' for each line.:confused:

Thanks

open my $FH, '<', '/tmp/a.txt' or die "Can't open a.txt: $!";
while ( my $line = <$FH> ) {
    chomp $line; # remove the newline at the end
    push @array, [ split ',', $line ];
}
close $FH;

Or (perlisher)

open my $FH, '<', 'a.txt';
@array = map { chomp; [ split ',' ] } <$FH>;
close $FH;

[/COLOR]:b: much much more faster now! Thanks :slight_smile: