Simple Question about Reading file by Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple Question about Reading file by Perl
# 1  
Old 11-10-2011
Simple Question about Reading file by Perl

Hi ,

I just write a simple function to read the file line by line.
But when I run it it says out of memory.
I am not sure about the root cause, Can someone help me out of this?
Smilie

Code:
#! /usr/bin/perl

use strict;

sub checkAPs{
        my $NDPDir = "/home/eweiqqu/NCB/NDP_files/";
        my $gsnFile = $NDPDir . "gsn.cbd";
        print $gsnFile."\n";
        my @aps = ();
        open Handler, $gsnFile or die "cannot open gsn.cbd";
        my @lines =<Handler>;
        while (@lines){
                push @aps, $3;
        }
        close(Handler);
        while (@aps){
                print $_." ";
        }
}

&checkAPs;

# 2  
Old 11-10-2011
Quote:
Originally Posted by Damon_Qu
...
I just write a simple function to read the file line by line.
But when I run it it says out of memory.
I am not sure about the root cause, Can someone help me out of this?

Code:
#! /usr/bin/perl

use strict;

sub checkAPs{
        my $NDPDir = "/home/eweiqqu/NCB/NDP_files/";
        my $gsnFile = $NDPDir . "gsn.cbd";
        print $gsnFile."\n";
        my @aps = ();
        open Handler, $gsnFile or die "cannot open gsn.cbd";
        my @lines =<Handler>;
        while (@lines){
                push @aps, $3;
        }
        close(Handler);
        while (@aps){
                print $_." ";
        }
}

&checkAPs;

You are **NOT** reading the file line by line. You are storing the entire file contents into an array called @lines. So if your file is huge, the program will run out of memory.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX File reading question

Is there a script that will automatically access/count the words in each individual file for a program? (Sorry, I'm trying to help my girlfriend who's in computer science out, I know nothing about this. We're having trouble wording this question.) (1 Reply)
Discussion started by: Triple M
1 Replies

2. Shell Programming and Scripting

Simple Perl question

Hello, I'm completely new to Perl and I'm just looking for a quick answer to some code I'm trying to come up with. I'm trying to access a website, part of the URL I want the user to be able to define via standard input. As you can see below I'm still trying to get the syntax. ... (2 Replies)
Discussion started by: wxornot
2 Replies

3. Shell Programming and Scripting

"Simple" echo/reading variable question...

Hello, I have a simple(I think) question! Although simple, I have been unable to resolve it, so I hope someone can help! OK, here it is: 1)I have an awk script that prints something, such as: awk '{print $2}' a > x so x might hold the value of say '10' 2)Now, I just want to check to see if... (4 Replies)
Discussion started by: astropi
4 Replies

4. UNIX for Dummies Questions & Answers

reading a file in Perl

If a form's action is the following Perl script how do I make it print the entire contents of the file on the screen? if(param()) { my $uploadedFile = param('file');#in the html page 'file' is the value of the name attribute of the input my $fh = upload($uploadedFile); ... (1 Reply)
Discussion started by: zerohour
1 Replies

5. Shell Programming and Scripting

Simple perl question

I am totally new to perl. I am modifying someone else's script. I have the following output: # ./some-perlscript A B C D E B - E, is generated through the print command that I put in the script. I want to remove A, it seems it is generated automatically by a custom OS it is querying when... (3 Replies)
Discussion started by: streetfighter2
3 Replies

6. Shell Programming and Scripting

PERL: simple comparing arrays question

Hi there, i have been trying different methods and i wonder if somebody could explain to me how i would perform a comparison on two arrays for example my @array1 = ("gary" ,"peter", "paul"); my @array2 = ("gary" ,"peter", "joe"); I have two arrays above, and i want to something like this... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

7. Shell Programming and Scripting

perl - reading from a file conditionally

Hi, I am new to perl. I want to read from a file on the basis of some conditions.. I want to define parameters in a configuration file in such a manner like... etc.. in my perl script, theer is a variable like this.. then i want to read values from first if block from the file... (1 Reply)
Discussion started by: shellwell
1 Replies

8. Shell Programming and Scripting

Reading File from Server - perl

Hey, I'm trying to read a file from a server. Simple file with some numbers. Here is the code i'm running. use Net::SSH::Perl::SSH1 ; $scon = Net::SSH::Perl->new ("com123.sever.mydomain.com",(protocol=>'2',port=>'22',debug=>'true')); $scon->login("user123","pass123"); open(FILE,... (9 Replies)
Discussion started by: Phi01
9 Replies

9. Shell Programming and Scripting

perl: simple question on string append

I want to append a decimal number to a string. But I want to restrict the number to only 2 decimal points for e.g: my $output = "\n The number is = "; my $number = 2.3333333; $output = $output . $number; But I want the $output as: "The number is = 2.33"; and not 2.3333333 (I do not... (1 Reply)
Discussion started by: the_learner
1 Replies

10. Shell Programming and Scripting

Perl Reading from File

is there a perl equivalent to sscanf? or something where I get the strings separated by spaces? (1 Reply)
Discussion started by: karyn1617
1 Replies
Login or Register to Ask a Question