in reply to Re^3: nested <FILE> read returns undefined?
in thread nested <FILE> read returns undefined?
I may be missing something (you haven't said what the second loop actually does, so I can't be sure), but couldn't this be better handled with next ?
Imagine a text file that has this format:
image: path/to/image.jpg attribute1: value attribute2: value [...] attributeN: value ---------- image: path/to/next/image.jpg [...]
Here, each "record" ends with a line of dashes. My script is reading the file, and when it reads an "image" line, it sees if that image is one that it knows about, and if not, it skips the rest of the record. So, the code looks like:
while (my $line = <FILE>) { $line =~ /(.*?): (.*?)$/m; ($key, $value) = (lc $1, $2); next if !$key || !$value; if ($key eq "image") { if (!exists $db->{$value}) { # not in the database, so skip this image while ($_ = <FILE>) { last if /^--/; } next; }
The original code used "for" instead of "while" in each spot above, which is why it wasn't working... and why I posted my original message.... I'm just answering why the code is the way it is because you asked. Sure, I could have done it in a way that allowed me to continue using "for", but changing it to use while() was a better option.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: nested <FILE> read returns undefined?
by tirwhan (Abbot) on Apr 01, 2006 at 08:28 UTC | |
by argv (Pilgrim) on Apr 01, 2006 at 18:43 UTC | |
|
Re^5: nested <FILE> read returns undefined?
by graff (Chancellor) on Apr 02, 2006 at 05:07 UTC |