Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Just to show the crowd that I can, in fact, whip-up a “one-liner” ...

perl -e 'use strict; use warnings; my $a="ABCDE"; my $b="xyz"; my $res +ult=""; my $i; for ($i=0; $i<length($b); $i++) { $result .= substr($a +, $i, 1) . substr($b, $i, 1) }; $result .= substr($a, $i); print "$re +sult\n";'

If you really, really, know that one string is always shorter than the other, then this problem simply consists of taking a character from both strings until the shorter string is exhausted.   Then, you append the remainder of the first (longer) string.

But I wouldn’t make such an assumption.   I would allow either string to be longer:

use strict; use warnings; sub zip { my ($a, $b) = @_; my $result = ""; my $max = ( length($a) > length($b) ) ? length($a) : length($b); my $i; for ($i = 0; $i < $max; $i++) { if ($i < length($a)) { $result .= substr($a, $i, 1); } if ($i < length($b)) { $result .= substr($b, $i, 1); } } print "$result\n"; } zip("XYZZY", "ABC"); zip("ABC", "XYZZY"); zip("XYZZY", "XYZZY"); zip("", ""); . . . XAYBZCZY AXBYCZZY XXYYZZZZYY

“Elegant?”   “Not?”   My response is the same as Rhett Butler’s.   The algorithm can be demonstrated to work in every case, by accompanying tests, and it is easy to eyeball it.

And, yes ... if I had any other reason to install and use an existing CPAN module that can zip a string, I would use that module.   I probably would not install it, just to zip a string, since the alternative is trivial.


In reply to Re: Merge 2 strings like a zip by sundialsvc4
in thread Merge 2 strings like a zip by tel2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-16 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found