mmcw
asked on
Get and add first line of file to array
I am reading big files.
First I read the totaly into memory and then did with that array what I had to do. This will cause memory problem with big files!
open(DATA,"$datafile")
@r_data = <DATA>;
close(DATA);
Searching the interne I found that it would be better to use the while loop. This will read line after line. It worked and my memory problems where gone.
open(DATA, $r_in->{'datafile'});
while (<DATA>) {
# do something with the data
# select specified lines
# and add them to the @r_data array.
push(@r_data,$_);
}
close(DATA);
The first line of the file (a flat database) contains the headers. How to read the first line of the file to the array @header using the while example?
First I read the totaly into memory and then did with that array what I had to do. This will cause memory problem with big files!
open(DATA,"$datafile")
@r_data = <DATA>;
close(DATA);
Searching the interne I found that it would be better to use the while loop. This will read line after line. It worked and my memory problems where gone.
open(DATA, $r_in->{'datafile'});
while (<DATA>) {
# do something with the data
# select specified lines
# and add them to the @r_data array.
push(@r_data,$_);
}
close(DATA);
The first line of the file (a flat database) contains the headers. How to read the first line of the file to the array @header using the while example?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$ctr = 0;
open(DATA, $r_in->{'datafile'});
while (<DATA>)
{
if($ctr == 1)
{
push(@r_data,$_);
}
else
{
push(@h_data,$_);
}
$ctr++;
}
close(DATA);
mmcw,
What's wrong with the answer I gave? If you tell us what problem you still have, we may be able to solve it.
What's wrong with the answer I gave? If you tell us what problem you still have, we may be able to solve it.
mmcw,
What's wrong with the answer I gave? If you tell us what problem you still have, we may be able to solve it.
What's wrong with the answer I gave? If you tell us what problem you still have, we may be able to solve it.
For your education, it's more efficient to read the header once rather than check the line number for every line read. With teraplane's code, you're checking every single line to see if it's the header line.
open(DATA, $r_in->{'datafile'});
$header = <DATA>;
chomp $header;
while (<DATA>) {
chomp;
# do something with the data
# select specified lines
# and add them to the @r_data array.
push(@r_data,$_);
}
close(DATA);