Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Limit loop

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


in reply to Limit loop

TIMTOWTDI:

  • use the LIMIT term of split: @favs = split /~~~/,$favorites, 25;
  • use a counter inside the loop, and last when the counter is above 25: state $counter=0; last if ++$counter>=25;
  • Replies are listed 'Best First'.
    Re^2: Limit loop
    by pryrt (Abbot) on Aug 30, 2022 at 21:55 UTC
      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.

    Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (6)
    As of 2023-03-28 22:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which type of climate do you prefer to live in?






      Results (70 votes). Check out past polls.

      Notices?