Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

next "for" question

Status
Not open for further replies.

kaih

Programmer
Dec 7, 1999
26
US
Another for loop that I would like to know why it does what it does....<br>
<br>
102: $pfile = &quot;/usr/local/test/testfile&quot;;<br>
... <br>
434: # make sure a path to $pfile exists.<br>
435: @p = split('/', $pfile);<br>
436: BUILDPATH: for ($d = &quot;/&quot; . shift(@p), $#p &gt; $[, $d .= &quot;/&quot; . shift(@p) ) {<br>
437 next BUILDPATH unless (-d $d); <br>
438: unless (mkdir($d, 0711)) {<br>
439: warn &quot;Cannot create path for bauetc. Cannot save file.&quot;;<br>
440: last BUILDPATH;<br>
441 }<br>
442 } # BUILDPATH<br>
<br>
In line 435 @p gets 5 elements &quot;&quot;, &quot;usr&quot;, &quot;local&quot;, &quot;test&quot;, &quot;testfile&quot;. But the first time I hit line 437 I see this:<br>
<br>
main::saveword(pscript:436): BUILDPATH: for ($d = &quot;/&quot; . shift(@p), $#p &gt; $[, $d .= &quot;/&quot; . shift(@p)) {<br>
DB&lt;1&gt; p join(&quot;-&quot;, @p)<br>
-usr-local-test-testfile<br>
DB&lt;2&gt; p $#p<br>
4<br>
DB&lt;3&gt; p scalar @p<br>
5<br>
DB&lt;4&gt; s<br>
main::saveword(pscript:437): next BUILDPATH unless (-d $d); <br>
DB&lt;4&gt; p $d<br>
//usr<br>
DB&lt;5&gt; p scalar @p<br>
3<br>
DB&lt;6&gt; <br>
<br>
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.<br>
<br>
What am I overlooking?<br>
<br>
Thanks again,<br>
- Kai.<br>
<br>
<br>
<br>

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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top