Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Decompose a String into Tuples (Faster and Compact Way)

by Tortue (Scribe)
on Oct 12, 2005 at 19:57 UTC ( #499659=note: print w/replies, xml ) Need Help??


in reply to Re: Decompose a String into Tuples (Faster and Compact Way)
in thread Decompose a String into Tuples (Faster and Compact Way)

(Um, for the above to do what the op wants, I think you need something like  push @ret, "@_[$i..$i+2]"; instead of  push @ret, [$_[$i], $_[$i+1], $_[$i+2]];)

Here's my shifty suggestion:

sub decomp { @_ = split / /, shift; return [ map { join " ", (shift, shift, $_[0]) } (1..@_/2) ]; }
update: shorter and generalizable to producing any size tuple:
sub decomp { my $n = 2; # for n+1-uples @_ = split / /, shift; return [ map { $_*=$n; "@_[$_..$_+$n]" } 0..@_/$n-1 ]; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://499659]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-08-18 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?