I have a data file, with each line having one number, like
10
20
30
40
How do I read this file and store the data into an array?
So that I can conduct some operations on this array.
This question is related to
perl
There is the easiest method, using File::Slurp
module:
use File::Slurp;
my @lines = read_file("filename", chomp => 1); # will chomp() each line
If you need some validation for each line you can use grep
in front of read_file
.
For example, filter lines which contain only integers:
my @lines = grep { /^\d+$/ } read_file("filename", chomp => 1);
Tie::File
is what you need:
Synopsis
# This file documents Tie::File version 0.98 use Tie::File; tie @array, 'Tie::File', 'filename' or die ...; $array[13] = 'blah'; # line 13 of the file is now 'blah' print $array[42]; # display line 42 of the file $n_recs = @array; # how many records are in the file? $#array -= 2; # chop two records off the end for (@array) { s/PERL/Perl/g; # Replace PERL with Perl everywhere in the file } # These are just like regular push, pop, unshift, shift, and splice # Except that they modify the file in the way you would expect push @array, new recs...; my $r1 = pop @array; unshift @array, new recs...; my $r2 = shift @array; @old_recs = splice @array, 3, 7, new recs...; untie @array; # all finished
It depends on the size of the file! The solutions above tend to use convenient shorthands to copy the entire file into memory, which will work in many cases.
For very large files you may need to use a streaming design where read the file by line or in chucks, process the chunks, then discard them from memory.
See the answer on reading line by line with perl if that's what you need.
I like...
@data = `cat /var/tmp/somefile`;
It's not as glamorous as others, but, it works all the same. And...
$todays_data = '/var/tmp/somefile' ;
open INFILE, "$todays_data" ;
@data = <INFILE> ;
close INFILE ;
Cheers.
open AAAA,"/filepath/filename.txt";
my @array = <AAAA>; # read the file into an array of lines
close AAAA;
Source: Stackoverflow.com