Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

printf "%s " x LIMIT, @a;

That does some of the job, but only uses the first four (LIMIT) elements. To process an array in chunks, you need some sort of loop. The loop has to use an array index, or must destroy the array while running. Your solution is more or less equal to:

print join ' ', @a[0..(LIMIT - 1)]; print " \n";
This leaves alle elements in the array, and the indexes are constant (although you flatten the entire array, the general idea is using the first four (LIMIT) items, leaving the array intact).

Solutions offered increase array indexes by four at a time, or destroy the array 4 elements at a time. The latter is done by looping until the array is empty (while it has elements), and spliceing to remove four elements. merlyn offered a compact solution that has the actual splice in the while-condition, which works because splice does not pad with undef elements, but actually returns an empty list when the array it spliced didn't have elements. The array assignment takes splices list and returns the array itself, which in scalar context evaluates to the number of elements. When the main array has no more elements, splice returns no elements, the array in scalar context returns 0, which is false so the loop stops.

There is (as far as I know) no way to solve this without a smart loop.

Yes, I reinvent wheels.


In reply to Re: Re: Breaking output lines into N-element chunks by Juerd
in thread Breaking output lines into N-element chunks by FoxtrotUniform

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found