Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Limit loop

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


in reply to Re: Limit loop
in thread Limit loop

To nitpick my own: the split-with-LIMIT put all the remaining favorites with ~~~ between (#25-30 in my example) in slot#25, which isn't what I intended. I changed the limit to 26, then dropped the last element if there were more than 25.

The code below shows that failure, plus the fixed version, plus the working counter-based solution.

#!perl use 5.012; # strict, // use warnings; my $favorites = join '~~~', map("t|k|$_", 1..30); # create data for SS +CCE print "initial favorites: $favorites\n"; print "="x40, "\n"; ##################################### { print qq#This was my original: it doesn't quite work, because the +25th will contain _all_ the remaining text \n#; my @favs = split /~~~/,$favorites,25; my $favnums = ""; foreach (@favs){ 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 "WRONG FOR 25+: favnums: $favnums\n"; print "="x40, "\n"; } ##################################### { print qq#This tweaks the limit\n#; my @favs = split /~~~/,$favorites,26; $#favs = 24 if @favs>25; # clips it to 25, so it throws away th +e 26th entry if there was one my $favnums = ""; foreach (@favs){ 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 "CORRECT LIMIT+TRIM: final favnums: $favnums\n"; print "="x40, "\n"; } ##################################### { print qq#This uses the counter\n#; my @favs = split /~~~/,$favorites; my $favnums = ""; foreach (@favs){ state $counter = 0; last if $counter++ >= 25; 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 "CORRECT COUNTER-based: final favnums: $favnums\n"; print "="x40, "\n"; } __END__ 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 ======================================== This was my original: it doesn't quite work, because the 25th will con +tain _all_ the remaining text 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~~~t|k|26~~~t|k|27~~~t|k|28~~~t|k|29~~~t|k|30 partial fa +vnums: 25~~~t,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5, +4,3,2,1, WRONG FOR 25+: favnums: 25~~~t,24,23,22,21,20,19,18,17,16,15,14,13,12, +11,10,9,8,7,6,5,4,3,2,1, ======================================== This tweaks the limit 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, CORRECT LIMIT+TRIM: 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, ======================================== This uses the counter 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|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, CORRECT COUNTER-based: 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, ========================================

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-16 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found