×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

next "for" question

next "for" question

next "for" question

(OP)
Another for loop that I would like to know why it does what it does....

102: $pfile = "/usr/local/test/testfile";
...
434: # make sure a path to $pfile exists.
435: @p = split('/', $pfile);
436: BUILDPATH: for ($d = "/" . shift(@p), $#p > $[, $d .= "/" . shift(@p) ) {
437 next BUILDPATH unless (-d $d);
438: unless (mkdir($d, 0711)) {
439: warn "Cannot create path for bauetc. Cannot save file.";
440: last BUILDPATH;
441 }
442 } # BUILDPATH

In line 435 @p gets 5 elements "", "usr", "local", "test", "testfile". But the first time I hit line 437 I see this:

main::saveword(pscript:436): BUILDPATH: for ($d = "/" . shift(@p), $#p > $[, $d .= "/" . shift(@p)) {
DB<1> p join("-", @p)
-usr-local-test-testfile
DB<2> p $#p
4
DB<3> p scalar @p
5
DB<4> s
main::saveword(pscript:437): next BUILDPATH unless (-d $d);
DB<4> p $d
//usr
DB<5> p scalar @p
3
DB<6>

Why did it shift twice? The initialization step should have shifted once, then the test should have let the loop happen, then AFTER the loop ran through the first time it should have shifted again in the increment step. But it looks to me like it is jumping straight from initialize to increment, and then to the loop.

What am I overlooking?

Thanks again,
- Kai.



RE: next "for" question

(OP)
It's because I tried to separate the for components with commas instead of semicolons. perl parsed it to look like
for (init1, init2, init3;;)

duh!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close