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";
}
| [reply] [d/l] |
|
|
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";
| [reply] [d/l] |
|
|
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.
| [reply] [d/l] [select] |
Re: Break an array. I think.
by Samy_rio (Vicar) on Nov 22, 2005 at 09:45 UTC
|
@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';
| [reply] [d/l] [select] |
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";
| [reply] [d/l] |
|
|
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 ]
| [reply] [d/l] |
|
|
| [reply] |
|
|
Re: Break an array. I think.
by LucaPette (Friar) on Nov 22, 2005 at 10:12 UTC
|
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);
| [reply] [d/l] [select] |
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;
}
| [reply] [d/l] |
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";
}
| [reply] [d/l] |