Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

while nested in foreach

by ear (Acolyte)
on Dec 01, 2003 at 14:18 UTC ( [id://311240]=perlquestion: print w/replies, xml ) Need Help??

ear has asked for the wisdom of the Perl Monks concerning the following question:

I have a while loop for a data file that is nested in a foreach loop (to look at 2 data files). My next statements inside the while loop are not working at all. They seem to be doing the next on the foreach instead of the while. Is there a way around this? I'm looking at raw test data and trying to skip data before 5pm the previous day and after 5pm of the current day. The substr is where the hour is located on the data line.
$day_counter = 0; foreach $dat_file ( @dat_file ) { open RAW, "<$dat_file"; while ( <RAW> ) { next (($day_counter == 0)&&(substr($_,9,2)<17)); next (($day_counter == 1)&&(substr($_,9,2)>16)); ($sum,$error) = 0; @numbers = split( '\t', $_ );

Replies are listed 'Best First'.
Re: while nested in foreach
by Abigail-II (Bishop) on Dec 01, 2003 at 14:25 UTC
    If I run this, I get:
    Label not found for "next " at /tmp/next line 9, <RAW> line 1.
    which doesn't quite surprise me. Are you sure you don't want an 'if' between the 'next' and the condition?

    Abigail

      Teach me to skip that second cup of coffee. I've been playing with this for 30 mins trying to figure out why it was only looking at the first line of data then stopping. I was sure it was doing the next on the filename istead of the raw data. Ugh. Thanks much, that of course fixed the problem.
Re: while nested in foreach
by AcidHawk (Vicar) on Dec 01, 2003 at 14:29 UTC

    From your description it seems as if you need an if between the next and the (($day...

    Something like

    next if (($day_counter == 0)&&(substr($_,9,2)<17)); next if (($day_counter == 1)&&(substr($_,9,2)>16));
    Update: D'oh Abigail-II was much faster with his reply.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
Re: while nested in foreach ($_ pitfalls)
by Russ (Deacon) on Dec 01, 2003 at 17:54 UTC
    When I read the subject ("while nested in foreach"), I thought this would be a different problem. Just for grins, here is a "gotcha" to watch for: resetting $_.
    for (1..10){ print "Before nested \$_ setter: $_\n"; open JUNK, 'junk' or die "Nope: $!"; while(<JUNK>){ # Do nothing, just let $_ get set } close JUNK; print "After nested \$_ setter: $_\n"; }
    In this example (perl 5.8.0), the second print statement shows an empty $_ because the diamond operator (in the while loop) overwrote $_ (as we told it to do). So, after the nested while loop, we no longer have the old value in $_ (the counter in the for loop).

    Exercises left for the reader: replace the while loop with other $_-using constructs and see how they behave...

    Most perl built-ins, and many CPAN modules, are "smart" enough to localize $_ to prevent confusion, but not all.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://311240]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found