Solved

Get and add first line of file to array

Posted on 2002-05-17
6
177 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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…
Six Sigma Control Plans

623 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