Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Limit loop

by pryrt (Abbot)
on Aug 30, 2022 at 21:44 UTC ( [id://11146517]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Limit loop
in thread Limit loop

Your script not working has nothing to do with whether or not the loop properly exits after a maximum of 25 loops.

You need to fix your bugs before declaring that someone's solution for a small question is invalid.

Thi spoiler contains an SSCCE that fixes (some) of your mistakes, with comments. But in brief, you didn't use strict and warnings, so you missed some of the problems; you initialized the wrong variable outside of your loop (which would have been caught by strict/warnings); and your vertical bar split had too many vertical bars, so it never split your loop variable into three variables (instead, putting the whole string in the $favtitle and leaving $favlink and $favnum as undef).

The code below shows the same loop working for both 5 favorites in the string (showing all) and for 30 favorites in the string (showing 25 of them).

#!perl use 5.012; # strict, // use warnings; for my $max ( 5, 30 ) { my $favorites = join '~~~', map("t|k|$_", 1..$max); # create data +for SSCCE print "initial favorites: $favorites\n"; ##################################### my @favs = split /~~~/,$favorites; my $favnums = ""; # note that you have the wrong variable ou +t here. this woiuld have been obvious if you had used warnings foreach (@favs[0..24]){ next unless defined; printf "fav: %s\t", $_; my ($favtitle,$favlink,$favnum) = split /\|/,$_; # you +had the wrong split expression; you only need one pipe in the split $favnums = $favnum . "," . $favnums; print "partial favnums: $favnums\n"; } print "final favnums: $favnums\n"; } __END__ initial favorites: t|k|1~~~t|k|2~~~t|k|3~~~t|k|4~~~t|k|5 fav: t|k|1 partial favnums: 1, fav: t|k|2 partial favnums: 2,1, fav: t|k|3 partial favnums: 3,2,1, fav: t|k|4 partial favnums: 4,3,2,1, fav: t|k|5 partial favnums: 5,4,3,2,1, final favnums: 5,4,3,2,1, initial favorites: t|k|1~~~t|k|2~~~t|k|3~~~t|k|4~~~t|k|5~~~t|k|6~~~t|k +|7~~~t|k|8~~~t|k|9~~~t|k|10~~~t|k|11~~~t|k|12~~~t|k|13~~~t|k|14~~~t|k +|15~~~t|k|16~~~t|k|17~~~t|k|18~~~t|k|19~~~t|k|20~~~t|k|21~~~t|k|22~~~ +t|k|23~~~t|k|24~~~t|k|25~~~t|k|26~~~t|k|27~~~t|k|28~~~t|k|29~~~t|k|30 fav: t|k|1 partial favnums: 1, fav: t|k|2 partial favnums: 2,1, fav: t|k|3 partial favnums: 3,2,1, fav: t|k|4 partial favnums: 4,3,2,1, fav: t|k|5 partial favnums: 5,4,3,2,1, fav: t|k|6 partial favnums: 6,5,4,3,2,1, fav: t|k|7 partial favnums: 7,6,5,4,3,2,1, fav: t|k|8 partial favnums: 8,7,6,5,4,3,2,1, fav: t|k|9 partial favnums: 9,8,7,6,5,4,3,2,1, fav: t|k|10 partial favnums: 10,9,8,7,6,5,4,3,2,1, fav: t|k|11 partial favnums: 11,10,9,8,7,6,5,4,3,2,1, fav: t|k|12 partial favnums: 12,11,10,9,8,7,6,5,4,3,2,1, fav: t|k|13 partial favnums: 13,12,11,10,9,8,7,6,5,4,3,2,1, fav: t|k|14 partial favnums: 14,13,12,11,10,9,8,7,6,5,4,3,2,1, fav: t|k|15 partial favnums: 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1, fav: t|k|16 partial favnums: 16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1 +, fav: t|k|17 partial favnums: 17,16,15,14,13,12,11,10,9,8,7,6,5,4,3, +2,1, fav: t|k|18 partial favnums: 18,17,16,15,14,13,12,11,10,9,8,7,6,5,4 +,3,2,1, fav: t|k|19 partial favnums: 19,18,17,16,15,14,13,12,11,10,9,8,7,6, +5,4,3,2,1, fav: t|k|20 partial favnums: 20,19,18,17,16,15,14,13,12,11,10,9,8,7 +,6,5,4,3,2,1, fav: t|k|21 partial favnums: 21,20,19,18,17,16,15,14,13,12,11,10,9, +8,7,6,5,4,3,2,1, fav: t|k|22 partial favnums: 22,21,20,19,18,17,16,15,14,13,12,11,10 +,9,8,7,6,5,4,3,2,1, fav: t|k|23 partial favnums: 23,22,21,20,19,18,17,16,15,14,13,12,11 +,10,9,8,7,6,5,4,3,2,1, fav: t|k|24 partial favnums: 24,23,22,21,20,19,18,17,16,15,14,13,12 +,11,10,9,8,7,6,5,4,3,2,1, fav: t|k|25 partial favnums: 25,24,23,22,21,20,19,18,17,16,15,14,13 +,12,11,10,9,8,7,6,5,4,3,2,1, final favnums: 25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6 +,5,4,3,2,1,


edit: struck about the vertical bar; the eventual follow-on data shows that there really are 3 bars between fields. Sorry.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-19 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found