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:
A little help still needed on line extraction

 



Clara
Novice


Jul 19, 2007, 6:05 PM

Post #1 of 4 (767 views)
A little help still needed on line extraction Can't Post

 
KevinR. Thank you very much.

I have really appreciated your help.

I got a little ambitious.

I thought of arranging each item on a line into a variable.

Can you help me with any pointers?




print "type your number";
$num=<STDIN>;
chomp($num);


open (DAT, 'info.txt') || die "unable to open file";
@line=<DAT>;



foreach $file (@line)
{

chop($file);
($code,$object,$color,$description,$comment)=split(/\,/,$file);

}



if ($file=~/^$num,/)

{
print "Code:$code,Object: $object,Color: $color,Description: $description,Comment: $comment";
}

Thanks in advance.


KevinR
Veteran


Jul 19, 2007, 6:55 PM

Post #2 of 4 (766 views)
Re: [Clara] A little help still needed on line extraction [In reply to] Can't Post

You could have continued this in your original thread.

Pointers: all perl scripts except the most trivial should include:

use strict;
use warnings;

this is especially true when you are first learning perl.

Your existing code is pretty good. Since you have divied the lines into seperate variables using split() you can check the ID number a different and more efficient way using the "==" math operator (because you are checking digits) instead of using a regexp:


Code
if ($code == $num) { 
print your stuff here
}

-------------------------------------------------


Clara
Novice


Jul 20, 2007, 5:31 AM

Post #3 of 4 (752 views)
Re: [KevinR] A little help still needed on line extraction [In reply to] Can't Post

 

Thanks for the pointer.

if ($code == $num) {
print your stuff here
}


The above code works but manages to print out only the last line in file all the time.

701,apple,red,round,cider
214,car,pink,sporty,cute,
229,knife,silver,sharp,cut

If a user inputs 701 or 214 nothing is printed out.
The program tends to print out only the last line if the beginning number or code of the particular line is correct.

In a nutshell the script is stuck on reading only from the last line.

Can i get some help in resolving this?

Thanks in advance.


KevinR
Veteran


Jul 20, 2007, 9:56 AM

Post #4 of 4 (745 views)
Re: [Clara] A little help still needed on line extraction [In reply to] Can't Post

If your code is essentially still written like this:


Code
foreach $file (@line) 
{

chop($file);
($code,$object,$color,$description,$comment)=split(/\,/,$file);

}



if ($file=~/^$num,/)

{
print "Code:$code,Object: $object,Color: $color,Description: $description,Comment: $comment";
}


the problem is you are checking to see if the two variables match after you have read through the entire array @line. The "if" condition needs to be inside the "foreach" loop:


Code
foreach $file (@line) { 

chomp($file);#<-- use chomp not chop
($code,$object,$color,$description,$comment)=split(/\,/,$file);

if ($code == $num) {
print your stuff here
}

}


when you put it outside the foreach loop $code can only ever equal the value of the last line of the array which is why you get the results you have been getting.
-------------------------------------------------

 
 


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

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