Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Merge 2 strings like a zip

by tel2 (Pilgrim)
on Jul 10, 2015 at 01:22 UTC ( [id://1134071]=note: print w/replies, xml ) Need Help??


in reply to Merge 2 strings like a zip

Thanks everyone for your contributions to my education.

I now realise my original code could have been simplified (and sped up) like this:

sub zip { my ($str1, $str2) = @_; my $zip; for (0 .. length($str2)-1) { $zip .= substr($str1, $_, 1) . substr($str2, $_, 1) } $zip .= substr($str1, length($str2)); return $zip; }

Of course that only meets my original spec's (i.e. 2nd string must be the shortest) but it seems to be pretty fast (though I guess the concatenation would slow it down if my data was much longer).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2024-03-28 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found