more useful options PerlMonks

### perlish mapping a list to triples

by water (Deacon)
 on Jun 06, 2004 at 01:34 UTC Need Help??
water has asked for the wisdom of the Perl Monks concerning the following question:

Hi --

I have list, @x, that is actually a flattened list triples:

```
@x = (( \$x1a, \$x1b, \$x1c), ( \$x2a, \$x2b, \$x2c), ( \$x3a, \$x3b, \$x3c), .
+.. etc ... (\$xna, \$xnb, \$xnc));
[download]```
What's the nice perlish to map this back to an LOL of triples:
```
[[\$x1a, \$x1b, \$x1c], [\$x2a, \$x2b, \$x2c], [\$x3a, \$x3b, \$x3c], ... etc .
+.. [\$xna, \$xnb, \$xnc]]
[download]```
I'm using an explicit loop with three  shifts to pull off the elements by threes. It works, but I'd like to learn the more idiomatic perlish solution. Thanks for the insight --

bottled water

Replies are listed 'Best First'.
Re: perlish mapping a list to triples
by dave_the_m (Prior) on Jun 06, 2004 at 01:38 UTC
something like:
```push @b, [splice @a, 0, 3 ] while @a;
[download]```
perhaps?

Dave.

Re: perlish mapping a list to triples
by BrowserUk (Pope) on Jun 06, 2004 at 02:12 UTC

If you don't want to destroy the original array with splice then you could do

```my @a = 1..30;
my @b = map{ [ @a[ \$_ .. \$_ +2 ] ] } map{ \$_ *3 } 0 .. \$#a/3;

print "@\$_" for @b;;
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
[download]```

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Re: perlish mapping a list to triples
by Django (Pilgrim) on Jun 06, 2004 at 11:56 UTC

Here's a variant of BrowserUks approach:

```my @a = (1..30);
my @b;
my \$p = 0;

push @b,[ @a[map{\$p++}(1..3)] ] while \$p < \$#a;

print map{"@\$_\n"}@b;
[download]```

-- Django
"Why don't we ever challenge the spherical earth theory?"

Re: perlish mapping a list to triples
by ambrus (Abbot) on Jun 06, 2004 at 18:28 UTC

You asked for a perlish way, so here is it. (Or at least this is my idea of perlish.) (A lispish way would be a tail-recursive function.)

```use Data::Dumper;
@arr= 1..18;

(@{\$aoa[@aoa]}[0..2], @arr) = @arr while @arr;
\$aoa= [@aoa];

print Dumper \$aoa;
[download]```

Update: hihi, I've just seen the sign under the post:

bottled water

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://361716]
Approved by Paladin
help
Chatterbox?
 [Discipulus]: many monks want to be hired tonight, other haired and some aired [Lady_Aleena]: Discipulus, do glob recurse? [Lady_Aleena]: s/do/does/; LanX wants to be fired [Discipulus]: i fear no [Discipulus]: i invented also 'gired' [Lady_Aleena]: Discipulus, then that is a problem. I wanted to find total seconds of my entire .mp3 collection to do some math on it to see how many days of continuous music i have. [LanX]: darn. .. I wanted to see Marine and Melonchon go to next round, just for fun xD [Discipulus]: LA use Perl: traversing a dir tree is simple; foreach file mp3 \$sec+= use Mp3::Info [Lady_Aleena]: Discipulus, I was able to get the sum of the seconds on the command line using awk. Then I ran the result through a perl script and got the result. It came bach with 4 days, 9 hours, 56 minutes, 43 seconds.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2017-04-23 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I'm a fool:

Results (432 votes). Check out past polls.