Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Limit loop

by htmanning (Friar)
on Aug 30, 2022 at 21:10 UTC ( [id://11146513]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: Limit loop
by pryrt (Abbot) on Aug 30, 2022 at 21:44 UTC
    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).


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

Re^4: Limit loop
by jwkrahn (Abbot) on Aug 30, 2022 at 21:57 UTC

    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 5.8.9.5 (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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (1)
As of 2024-04-18 04:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found