CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Stuck printing lines selectively

 



jgrilo
New User

Feb 8, 2010, 11:34 PM

Post #1 of 6 (1408 views)
Stuck printing lines selectively Can't Post

Hi all,

I am totally new to Perl.

I am trying to do the following. I have a file with several thousands of lines. The lines I am intersted in have the following format:

Cp**** string1 string2 number

(The * are all digits)

I want to detect these lines and print out those in which "number" satisfies some criterion (>, <, etc).

This is what I am doing:



Code
 #!/usr/bin/perl   

open(fin, $ARGV[0]);
my @file_content = <fin>;
close(fin);

my $cap_value=$ARGV[1];

foreach $line (@file_content){
@array = split(/" "/,$line);
if ($array[0] =~ /Cp\d?/ ){
print $line;
}
}



The code allows me to print (only) the entire line, regardless of the value in "number."
I have tried to introduce a conditional statement to look at $array[3] and only then print the line but nothing seems to work.
Apparently, the size of @array after the split is 1, not whatever the number of fields separated by a space.

I would appreciate any suggestions on how to approach the problem.

Thanks!


(This post was edited by jgrilo on Feb 9, 2010, 12:27 AM)


7stud
Enthusiast

Feb 9, 2010, 12:03 AM

Post #2 of 6 (1405 views)
Re: [jgrilo] Stuck printing lines selectively [In reply to] Can't Post

google 'code tags'. Read the first ten results. Then come back and post what you have learned.


jgrilo
New User

Feb 9, 2010, 12:28 AM

Post #3 of 6 (1399 views)
Re: [7stud] Stuck printing lines selectively [In reply to] Can't Post

Thanks. I hope it's better now.


7stud
Enthusiast

Feb 9, 2010, 1:58 AM

Post #4 of 6 (1395 views)
Re: [jgrilo] Stuck printing lines selectively [In reply to] Can't Post


Code
split(/" "/,$line);


That says to split $line wherever $line contains the three characters: a double quote, a blank space, and a double quote. Because that series of 3 characters never appears in $line, $line isn't split into pieces, and the array has one element: the original line.

The first two lines of every program should be:


Code
use strict; 
use warnings;


If you include a shebang line, the shebang line comes before those lines.

This regex:

/Cp\d?/

is equivalent to:

/Cp/

They will both match the strings:

'Cp'
'Cp8'
'Cp123'

regexes will happily match nothing before trying to match something.


(This post was edited by 7stud on Feb 9, 2010, 2:08 AM)


jgrilo
New User

Feb 9, 2010, 9:28 AM

Post #5 of 6 (1373 views)
Re: [7stud] Stuck printing lines selectively [In reply to] Can't Post

Thanks! The comments on split (/ / vs/ /" "/) solved the problem.

I get a warning message that the unquoted string "fin" may clash with future reserved words.

Is the syntax incorrect?

Thanks.


7stud
Enthusiast

Feb 9, 2010, 1:55 PM

Post #6 of 6 (1363 views)
Re: [jgrilo] Stuck printing lines selectively [In reply to] Can't Post

The syntax is technically correct, however I think perl is telling you that you chose a bad name for your filehandle. In any case, modern perl dictates that you:

1) Declare all variables--usually with 'my'.

2) Use the three argument form of open().

3) Use a my variable for the filehandle rather than a bareword.

4) Capitalize the filehandle variable name.

For example:

Code
open my $INFILE, '<', 'file.txt' or die "Couldn't open file.txt: $!"; 
my @file_content = <$INFILE>;


It's also considered good style to omit parentheses for perl's built in functions to distinguish them from calls to user defined subroutines.

Also be aware that if your file is big, reading the file into memory all at once may overwhelm your system's memory. If you just need to process the file line by line, then you can do this:

Code
while (my $line = <$INFILE>) { 

}

In that case, only one line at a time is read into memory.


(This post was edited by 7stud on Feb 9, 2010, 5:08 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives