Re: Processing pairs of values from even-sized array
by AnomalousMonk (Archbishop) on Jun 03, 2011 at 12:30 UTC
|
while(@arr) {
my ($x,$y) = (shift(@arr), shift(@arr));
...
}
Update: ... looks [like] the each generator ...
Maybe (see List::MoreUtils):
use List::MoreUtils qw(natatime);
;;
my $it = natatime 2, @arr;
while (my ($x, $y) = $it->()) {
...
}
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
Re: Processing pairs of values from even-sized array
by Fletch (Bishop) on Jun 03, 2011 at 12:41 UTC
|
List::MoreUtils has pairwise and natatime, the former which works like a two-at-a-time map and the later makes an iterator that you kick in a while loop.
Example:
my @x = ('a' .. 'g');
my $it = natatime 3, @x;
while (my @vals = $it->())
{
print "@vals\n";
}
This prints
a b c
d e f
g
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] [select] |
Re: Processing pairs of values from even-sized array
by johngg (Canon) on Jun 03, 2011 at 12:30 UTC
|
knoppix@Microknoppix:~$ perl -E '
> @arr = ( 1 .. 6 );
> while ( @arr )
> {
> ( $x, $y ) = splice @arr, 0, 2;
> say qq{$x - $y};
> }'
1 - 2
3 - 4
5 - 6
knoppix@Microknoppix:~$
I hope this is helpful.
| [reply] [d/l] |
Re: Processing pairs of values from even-sized array
by oxone (Friar) on Jun 03, 2011 at 12:40 UTC
|
There's also a 'pairwise' function in List::MoreUtils, which might be worth a look. | [reply] |
Re: Processing pairs of values from even-sized array
by Bloodnok (Vicar) on Jun 03, 2011 at 12:32 UTC
|
my $array = { @array };
while (my ($k, $v) = each %$array) {
.
.
}
Or possibly simpler still, but I haven't yet worked out how to perform an in-line cast of a list to an array - while (my ($k, $v) = each %{@array}) {... generates errors coz the list is being evaluated in scalar, not list, context ... JIC you were wondering:-)
A user level that continues to overstate my experience :-))
| [reply] [d/l] [select] |
|
my $array = { @array };
This will 'randomize' (or 'hash-ize'?) the order of elements in the array, and %{{@array}} likewise. Update: Oops: each %{{@arr}} has another, entirely different, fatal (or should I say immortal?) flaw!
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] [d/l] |
|
Interestingly, the 5.12+ each allows iteration over an array (and similarly for keys and values), but each 'key' is a succcessive index of the array, and each 'value' is the element at that index: not quite what you wanted in the OP.
>perl -wMstrict -le
"my @ra = qw(a b c d);
;;
while (my ($i, $element) = each @ra) {
print qq{ra $i is '$element'};
}
"
ra 0 is 'a'
ra 1 is 'b'
ra 2 is 'c'
ra 3 is 'd'
| [reply] [d/l] |
Re: Processing pairs of values from even-sized array
by ciderpunx (Vicar) on Jun 03, 2011 at 15:56 UTC
|
my @arr = qw/1 2 3 4 5 6 7 8/;
while(my ($x,$y) = splice @arr,0,2) {
say "$x => $y";
}
Something I've needed in the past was to get two arrays (odds and evens). Grep can do this. You could combine them into a hash thus:
my @arr = qw/1 2 3 4 5 6 7 8/;
my ($j,$i)=(0,0);
my ($x,$y) = ([grep { ++$i % 2} @arr] , [grep {$j++ % 2} @arr]);
my %hash;
@hash{@{$x}}=@{$y};
for(keys %hash) {
say "$_ => $hash{$_}"
}
Gives us:
1 => 2
3 => 4
7 => 8
5 => 6
Or you could iterate them (which is safer if some x may be undef)
my @arr = qw/1 2 3 4 5 6 7 8/;
push @arr, undef;
push @arr, 9;
my ($j,$i)=(0,0);
my ($x,$y) = ([grep { ++$i % 2} @arr] , [grep {$j++ % 2} @arr]);
$i=0;
for(@{$x}){
say $_ ." => " . $y->[$i++] if($_);
}
Obviously that doesn't scale to big arrays. I'm sure List::MoreUtils is always the better approach.
| [reply] [d/l] [select] |
|
knoppix@Microknoppix:~$ perl -E '
> push @{ $_ % 2 ? \ @odds : \ @evens }, $_ for 1 .. 8;
> say qq{ Odds : @odds};
> say qq{Evens : @evens};'
Odds : 1 3 5 7
Evens : 2 4 6 8
knoppix@Microknoppix:~$
I hope this is of interest.
| [reply] [d/l] |
|
>perl -wMstrict -le
"use List::MoreUtils qw(part);
use Data::Dumper;
;;
my $i;
my @parts = part { ++$i % 2 } 1 .. 8;
;;
print Dumper \@parts;
"
$VAR1 = [
[
2,
4,
6,
8
],
[
1,
3,
5,
7
]
];
| [reply] [d/l] [select] |
|
|
Re: Processing pairs of values from even-sized array
by sundialsvc4 (Abbot) on Jun 03, 2011 at 16:52 UTC
|
Pick a way that works, make it clear as a bell, add a comment and move on...
| [reply] |