in reply to printing 20 characters in a line.

Hi dee! ; )

maybe using splice() makes it shorter and more maintainable:

use strict; use warnings; my $str="30 30 36 33 36 36 36 33 36 34 26 24 21 17 17 23 27 27 31 31 3 +4 33 33 33 36 36 37 38 38 38 38 38 35 40 34 34 34 31 37 36 37 37 40 4 +0 49 40 40 40 40 40 40 40 40 33 30 30 30 30 30 42 45 45 45 45 45 49 4 +2 42"; $str=~s/\s*\+//gs; # get rid of linebreaks and these useless pluses my @array=split(/\s+/,$str); # (*) while ( my @chunk = splice(@array,0,20) ) { print "@chunk\n"; }
tested.

cheers
Rolf

UPDATES:
(*) splitting /\s+/ makes it more stable than just / /

---
Which song? ;)

Replies are listed 'Best First'.
Re: thats what splice() is for...
by wfsp (Abbot) on Nov 14, 2008 at 12:52 UTC
    ...these useless pluses
    are put there by the monastery plumbers (which is why they are red). If you download the code using the download link you can avoid them (and the string is all on one line).
      >(which is why they are red)
      Well I'm daltonian, now that you told me I realise it's red! : )

      I'm not sure if there is no better method which would preserve perlsyntax after cut&paste, maybe backslashes at the end of an wraped line!

      hmmm...well this discussion can't be new I'll supersearch for it!

      ---
      Which song? ;)
        Use the per snippet [download] link, use the per post [d/l] link, or set your CSS to wrap code blocks using scroll bars.
        Use the force... erm, the [download] link, Luke!
        The link below! Link!!!
        []s, HTH, Massa (κς,πμ,πλ)
splice without splice()
by LanX (Saint) on Nov 14, 2008 at 11:58 UTC
    Here an alternative version:

    same idea, but without splice() by using more perlish features like list-flattening and arrayslicing

    my @chunk; while ( ( @chunk[0..19] , @array ) = @array ) { print "@chunk\n"; }
    but I think splice is better maintainable!

    UPDATE: Indeed, real splices are better, see "Good" style for splicing arrays for details.

    ---
    Which song? ;)