in reply to Merge 2 strings like a zip

#!/usr/bin/perl # http://perlmonks.org/?node_id=1133857 use warnings; use strict; print zip("ABCDEFGHIJ", "abcde"), "\n"; sub zip { my ($str1, $str2) = @_; $str1 =~ s/.\K/ substr $str2, 0, 1, ''/gesr; }

Replies are listed 'Best First'.
Re^2: Merge 2 strings like a zip
by tel2 (Pilgrim) on Jul 09, 2015 at 05:24 UTC
    Thanks for that, Anonymous Monk!

    I don't fully understand it yet, but it looks beautiful. 8)

    That's the first time I've seen /K and /r.  No problems with /K, but my webhost is still using v5.10.1 and I see that /r didn't come in until 5.14.  So how could I write that without the /r modifier?  I think I understand what /r is supposed to do, but I don't yet understand your code enough to replace it in this context.

    I've added a 2nd (unzip) function request, below, which I'd appreciate your input on, if you have time.

    Thanks again.

      zip() without /r and unzip()

      #!/usr/bin/perl # http://perlmonks.org/?node_id=1133857 use warnings; use strict; print zip("ABCDEFGHIJ", "abcde"), "\n"; print "@{[ unzip('AaBbCcDdEeFGHIJ', 5) ]}\n"; sub zip # if no /r { my ($str1, $str2) = @_; $str1 =~ s/.\K/ substr $str2, 0, 1, ''/ges; $str1 } sub unzip { my ($input, $length2) = @_; return join('', unpack "(ax)$length2 a*", $input), join '', unpack "(xa)$length2", $input; }
        Nice work, Anonymous Monk!

        Thanks again.