Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of ambs (3914)

Thursday July 08, 2004
01:24 PM


[ #19755 ]
I work at a University (well, I'm preparing to start a PhD). Meanwhile, I've been helping giving an exam (programming and C). One of the questions was to write a zip function: given two strings with the same size (say, "abcd" and "efgh"), print the zipped version ("aebfcgdh").

During the exam I (and other Perl programmer) were talking about how to do it in Perl, but different: $a and $b has each string, and we want to create in memory the zipped string.

I came across with this (I think, interesting) solution:

@b = split //, $b;
$a =~ s!.!$&.shift@b!ge

Other suggestions of "interesting" ways to do it?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • $a=~s/(?<=.)/$b=~m|.|g;$&/ge;
    • hmms... nice bunch of characters :-) Interesting result :-) Perl rockz :-) You Rockz :)
      • Actually I originally did it like this, just to have a substitution inside a substitution:


        But I decided that destroying $b in the process might not be acceptable.

  • sub zip {
        my( $a, $b ) = @_;
        my $res;
        $res .= chop($b).chop($a) for 1..length($a);
        scalar(reverse $res);
    • Nice. We all forget chop :-)
      • I long ago proposed, on perl5-porters, an operator "chip" that would take one char from the front of a string, the way chop takes a character from the end. That would get around the need to reverse the chopped together $res.
        • Chip sounds cool... can remember computer chips but also food O:-)

          During our discussion about an "interesting" solution, we could take advantage of chip. We thought of unshift, but that doesn't work for arrays.

          • The computer chip I referred to in the original proposal was Chip Salzenburg, who was working on Topaz (the original potential perl6) at the time, had established The Perl Foundation, and was the original pumpking (after Larry), so sneaking an honorarium mention of him into the language seemed reasonable.
          • We thought of unshift, but that doesn't work for arrays. s/arrays/strings/;

            Much like your original proposal, you can use:

            @a = split //, $a;
            @b = split //, $b;
            my $res .= (shift @a) . (shift @b) while @a;
            • That was almost my first solution:
              @a = split //, $a;
              @b = split //, $b;
              while (@a) {
                push @c, shift(@a), shift(@b)
              $c = join "",@c;
  • Just wait a while, perl6 will have a zip operator (the Yen currency symbol) that merges two lists in interleaved order.
  • $b=~s!!$a=~/./g;$&!ge

    The zip in my solution is in $b.

    Casey West