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: Intermediate:
problem with while loop and reg expressions on file

 



taylor
Novice

Jan 19, 2007, 7:26 PM

Post #1 of 6 (673 views)
problem with while loop and reg expressions on file Can't Post

Hi all, I have a file of data that I am looping through.

And I do something like this.
While (<FILE>) {

if (/^Heading1/) {
do some stuff;
}

if (/^---/) {
$value="NULL";
}

if (/^(\d+/.\d+/) {
$number=$1;
print "number is $number\n";
}

}#end of while
And this works just fine. print statement shows value of $number for each match.

But If I add right after this, the exact same code block within the while loop but change Heading1 to Heading2 so I now have ...

While (<FILE>) {

if (/^Heading1/) {
do some stuff;
}

if (/^---/) {
$value="NULL";
}

if (/^(\d+/.\d+/) {
$number=$1;
print "number is $number\n";
}

if (/^Heading2/) {
do some stuff;
}

if (/^---/) {
$value="NULL";
}

if (/^(\d+/.\d+/) {
$number=$1;
print "number is $number\n";
}

}#end of while

Then my print statements still work, but they print 2 of everything they find. If I add a third heading regex, it prints 3 and so on. My question is why is it doing that? I thought the while loop would read line by line, so it should just go on to the next line, and disregard what its seen before.

Thanks,
Taylor


taylor
Novice

Jan 19, 2007, 7:45 PM

Post #2 of 6 (671 views)
Re: [taylor] problem with while loop and reg expressions on file [In reply to] Can't Post

As an example to show the results of what I was trying to say..

With only one heading regex, I get this...

number is 10.05
number is 11.23

But when I add the second regex heading search, I get this...

number is 10.05
number is 10.05
number is 11.23
number is 11.23


KevinR
Veteran


Jan 19, 2007, 8:09 PM

Post #3 of 6 (665 views)
Re: [taylor] problem with while loop and reg expressions on file [In reply to] Can't Post

In the future please use the code tags to display code and post some sample data.

You probably want to use an if/elsif/elsif block of conditions to find the different headings then proceed to doing the other things.


Code
if (conditon) { 
do only this stuff
}
elsif(condition) {
do only this stuff
}
elsif(condition) {
do only this stuff
}


that way only the first condition that returns true is acted on, the rest are ignored. You can embed more 'if' or 'elsif' conditons inside the primary conditions if need be.
-------------------------------------------------


taylor
Novice

Jan 20, 2007, 9:13 PM

Post #4 of 6 (654 views)
Re: [KevinR] problem with while loop and reg expressions on file [In reply to] Can't Post

I'm not sure that will work, because when I find a given heading, I need to make note of it, then continue parsing line by line and storing info.

For instance, the text in the file I have not including some markup tags looks like this...

Cash
---
---
12.00
2.00
---
---
5.00

Inventory
---
---
5.00
6.00
7.00
8.00
9.00

The --- represent no entry, not 0.00, but there can be a variable number of lines of other markup crap in between the lines I'm interested in. So I am trying to loop through until I find a heading I like, then loop through and assign the results to variables, continue on until next heading, and repeat...


KevinR
Veteran


Jan 21, 2007, 10:12 AM

Post #5 of 6 (647 views)
Re: [taylor] problem with while loop and reg expressions on file [In reply to] Can't Post

You could probably use embedded "while" loops and labels. Very generic example:


Code
MAINLOOP: while (<FILE>) { 
some initial processing maybe;
if (some condition) {
while (<FILE>) {
do somthing;
next MAINLOOP if (some condition);
}
}


the above is similar to a while/continue construct but I find labels are easier to understand.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Jan 22, 2007, 5:16 AM

Post #6 of 6 (641 views)
Re: [taylor] problem with while loop and reg expressions on file [In reply to] Can't Post

Why duplicate the code that prints the number? If you only want the number to appear once, then just have the code once.

I think you're a bit confused about how if statements work. You probably want to look at if/else or if/elsif as KevinR suggested.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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