Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Merge 2 strings like a zip

by kcott (Archbishop)
on Jul 09, 2015 at 05:14 UTC ( [id://1133877]=note: print w/replies, xml ) Need Help??


in reply to Merge 2 strings like a zip

G'day tel2,

Here's my offering. It just uses length and substr. There's no: concatenation; splitting strings into lists; regexes; or modules to load.

#!/usr/bin/env perl -l use strict; use warnings; print zip("ABCDEFGHIJ", "abcde"); sub zip { my ($str1, $str2) = @_; for (0 .. length $str2) { substr $str1, $_ * 2 + 1, 0, substr $str2, $_, 1; } return $str1; }

Output:

AaBbCcDdEeFGHIJ

-- Ken

Replies are listed 'Best First'.
Re^2: Merge 2 strings like a zip
by tel2 (Pilgrim) on Jul 09, 2015 at 07:07 UTC
    G'day mate.

    Awesome work from across the ditch!

    Thanks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-23 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found