vox2000 has asked for the wisdom of the Perl Monks concerning the following question:

I have an array like this:
@datan = ("1","2","3","4","5","6");

And I print it like this:
print join(" ",@datan);

Very good. But if I want to break the array at number 3, so I have a block like this:

1 2 3
4 5 6

How do I do that? I'm fairly new to perl, but it like it so far.

Thanks

Replies are listed 'Best First'.
Re: Break an array. I think.
by borisz (Canon) on Nov 22, 2005 at 09:39 UTC
    @datan = ("1","2","3","4","5","6"); while ( my @x = splice(@datan, 0, 3) ) { print join(" ",@x), "\n"; }
    Boris
      This will destroy the original array! To leave it intact you can do:
      @datan = ("1","2","3","4","5","6"); my $i = 0; while ( (my @x = @datan[(0+$i)..(2+$i)]) && $i<$#datan ) { print join(" ",@x), "\n"; $i+=3; } print "l: @datan";


      holli, /regexed monk/
        Or a little more simply,
        for my $i (0 .. $#datan/3) { print join(' ', @datan[$i*3 .. $i*3 + 2]), "\n"; }
        It looks to me like you introduced @x so that splice wouldn't destroy the original array, but then opted not to use splice after all.

        Caution: Contents may have been coded under pressure.
Re: Break an array. I think.
by Samy_rio (Vicar) on Nov 22, 2005 at 09:45 UTC

    Hi vox2000, Try this,

    @datan = ("1","2","3","4","5","6"); print map{($_ eq "3")?("$_\n"):("$_ ")} @datan; __END__ 1 2 3 4 5 6

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Break an array. I think.
by vox2000 (Initiate) on Nov 22, 2005 at 10:51 UTC
    Thanks all. This is how it ended up:

    my @datan = qw(1 2 3 4 5 6); for my $meck(@datan) { print "$meck "; print "\n" if $meck eq 3; } print "\n";
      You picked my least favorite example of all the ideas presented here. (In fact, I voted it down as being harmful advice.) That is checking each element to see if it exactly matches the ONE element which needs a break following ($mech eq "3"). It's very inflexible:
      • if you need to change where the newline happens, you have to edit the loop code,
      • if you change your data, you have to edit the loop code,
      • if you need more than one newline, you have to edit the loop code,
      • if your data has more than one such element, you have to edit the loop code,
      • and so on.
      Good algorithms work in terms of the most general solution, so that you don't have to keep going in there and changing the code. Every change to the code means another chance of getting it wrong, more testing and debugging, and being hit by a bug later which testing didn't find.

      --
      [ e d @ h a l l e y . c c ]

        Okey okey, thank you for the advice. I am just a new perl-deciple, trying stuff out. I was just looking for a way to do it, in a simple way. And maybe later, when more experienced, expand it to some extent.

        Which solution would you use? Or do you have your own way to do it?

        Thanks

Re: Break an array. I think.
by LucaPette (Friar) on Nov 22, 2005 at 10:12 UTC
    Hi vox2000,

    About:

    print join(" ",@datan);
    you can obtain the same behavoir more simply with:
    print "@datan";
    But beware the value of $" special variable.
    Infact using a naked block and localizing $" you can get very nice behavoir. Try:
    { local $"=", "; print "@datan" }
    Moreover, according to TMTOWTDI, you can "break" your array also with:
    my $break=3; print $break!=$_ ? "$_ ": "$_\n" for (@datan);
Re: Break an array. I think.
by tphyahoo (Vicar) on Nov 22, 2005 at 09:49 UTC
    use strict; use warnings; my @datan = qw(1 2 3 4 5 6); for my $element (@datan) { print "$element "; print "\n" if $element eq 3; }
Re: Break an array. I think.
by TedPride (Priest) on Nov 22, 2005 at 10:43 UTC
    use strict; use warnings; my @datan = ("1","2","3","4","5","6"); for ($_ = 0; $_ <= $#datan; $_ += 3) { print "@datan[$_..($_+2)]\n"; }