Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Joining an array

by trizen (Hermit)
on Feb 11, 2012 at 11:36 UTC ( #953220=note: print w/ replies, xml ) Need Help??


in reply to Joining an array

Here is a splice + join version...

my @t = qw(name John number 7 status unknown); my $i = -1; foreach my $x (1 .. $#t) { splice(@t, $i += 2, 0, $x % 2 ? '=' : '&'); } print join '', @t;


Comment on Re: Joining an array
Download Code
Replies are listed 'Best First'.
Re^2: Joining an array
by trizen (Hermit) on Feb 12, 2012 at 01:56 UTC
    A faster way:
    my @array = qw(name John number 7 status unknown); my $i = -2; my $x = $#array - 1; my $str = ''; while (1) { if ($i + 3 < $x) { $str .= $array[$i += 2] . '=' . $array[$i + 1] . '&'; } else { $str .= $i + 3 == $x ? $array[-3] . '=' . $array[-2] . '&' . $arra +y[-1] : $i + 2 == $x ? $array[-2] . '=' . $array[-1] : $array[-1]; last; } } print "$str\n";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2015-07-07 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls