Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get and add first line of file to array

Posted on 2002-05-17
6
Medium Priority
?
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Independent Software Vendors: 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!

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans

721 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