Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: Limit loop

by pryrt (Abbot)
on Aug 30, 2022 at 20:59 UTC ( #11146512=note: print w/replies, xml ) Need Help??

in reply to Re: Limit loop
in thread Limit loop

            foreach ( @favs[ 0 .. 24 ] ) {

Since the original post said only "a few of these favorite fields have more than 25", then most of the time, that will end up with one or more undef iterations on the loop, so then undef would have to be handled specially. Maybe via next unless defined; or similar logic.

Replies are listed 'Best First'.
Re^3: Limit loop
by htmanning (Friar) on Aug 30, 2022 at 21:10 UTC
    Thanks but this didn't work. It just throws the same end of script output before header error. And YES, most of them have less than 25 favs but some do have more. Most of the time this is a non issue.
      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.

      Before the loop remove the excess elements

      splice @favs, 25; foreach (@favs){
        splice @favs, 25;

        Note that splice-ing past the end of the array results in a warning in earlier Perl versions:

        Win8 Strawberry (32) Wed 08/31/2022 15:05:47 C:\@Work\Perl\monks >perl -wMstrict -MData::Dump=dd -le "my @ra = ('a' .. 'd'); splice @r +a, 10; dd \@ra" splice() offset past end of array at -e line 1. ["a" .. "d"]
        (I don't know with what version this warning was suppressed.)

        Give a man a fish:  <%-{-{-{-<

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2023-05-31 16:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found