in reply to Re: Break an array. I think.
in thread Break an array. I think.

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: 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 ]

Replies are listed 'Best First'.
Re^3: Break an array. I think.
by vox2000 (Initiate) on Nov 22, 2005 at 14:44 UTC
    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

      It seems to me you don't really want your break to be determined by the value you're breaking on, but on it being the third number.
      my @datan = qw(1 2 3 4 5 6); for ($cnt=0; $cnt<=$#datan; $cnt++) { print "\n" if $cnt%3 == 0;## remainder==zero after division by 3 print "$datan[$cnt] "; } print "\n";
      Using this you could arbitrarily increase the number of elements, not worry about their values, and it would always give you three elements to a row. To get them to line up, unless you're assured of using single digits, you'd probably either want to use sprintf or at least separate them by tabs.