Solved

Get and add first line of file to array

Posted on 2002-05-17
6
169 Views
Last Modified: 2010-03-05
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?

0
Comment
Question by:mmcw
  • 4
6 Comments
 
LVL 7

Expert Comment

by:yoren
ID: 7015757
Try the following code. I've added "chomp" to remove the new line characters from the ends of the lines. Also note that I've made $header a scalar variable rather than an array, since it has only one 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);


0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 50 total points
ID: 7015760
Use the special perl variable $. to track the input file line number

while (<DATA>)
{
        if ( $. == 1 )
     {
          push(@h_data,$_);
     }
     else
     {
          push(@r_data,$_);
     }
}
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 7016613

$ctr = 0;

open(DATA, $r_in->{'datafile'});
while (<DATA>)
{
 if($ctr == 1)
 {
   push(@r_data,$_);
 }
 else
 {
   push(@h_data,$_);
 }
 $ctr++;
}
close(DATA);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:yoren
ID: 7016779
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.
0
 
LVL 7

Expert Comment

by:yoren
ID: 7016862
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.
0
 
LVL 7

Expert Comment

by:yoren
ID: 7016937
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question