Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Merge 2 strings like a zip

by 1nickt (Canon)
on Jul 09, 2015 at 02:42 UTC ( [id://1133862]=note: print w/replies, xml ) Need Help??


in reply to Merge 2 strings like a zip

Use a CPAN module! Preferably one that has a method that does what you want, and is called what you imagine it should be :-)

#!/usr/bin/env perl use strict; use warnings; use List::MoreUtils qw/ zip /; my @a1 = split('', 'ABCDEFGHIJ'); my @a2 = split('', 'abcde'); no warnings qw/ uninitialized /; print zip @a1, @a2; use warnings; __END__
Remember: Ne dederis in spiritu molere illegitimi!

Replies are listed 'Best First'.
Re^2: Merge 2 strings like a zip
by BrowserUk (Patriarch) on Jul 09, 2015 at 03:11 UTC

    Hm.

    #! perl -slw use strict; use Benchmark qw[ cmpthese ]; use List::MoreUtils qw[ zip ]; sub zipA { my( $str1, $str2 ) = @_; $str1 =~ s/.\K/ substr $str2, 0, 1, ''/gesr; } sub zipB { no warnings qw/ uninitialized /; my( $a, $b ) = @_; my @a1 = split( '', $a ); my @a2 = split( '', $b ); return join'', zip @a1, @a2; } sub zipC($$){ my( $n, $a, $b ) = ( 1, @_ ); substr( $a, $n, 0, $_), $n += 2 for split '', $b; return $a; };; our $A = 'ABCDEFGHIJ'; our $B = 'abcde'; cmpthese -1, { A => q[ my $zipped = zipA( $A, $B ); ], B => q[ my $zipped = zipB( $A, $B ); ], C => q[ my $zipped = zipC( $A, $B ); ], }; __END__ C:\test>\perl5.18\perl\bin\perl.exe 1133857.pl Rate B A C B 43932/s -- -48% -72% A 84167/s 92% -- -47% C 159444/s 263% 89% --

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
      "substr( $a, $n, 0, $_), $n += 2 for split '', $b;"

      Excellent use of substr

      The race is not always to the swift, my friend.

      Remember: Ne dederis in spiritu molere illegitimi!

        1nickt:

        If it's a race, I'd expect the swift to win... ;^)

        On a more serious note: Pulling in a module for such a simple task doesn't seem as good as simply making a simple subroutine and giving it a good name.

        Certainly, for more intricate operations, modules can be very useful. But if you need a particular small routine (like this one), and it servers a particular purpose correctly (like this one), and it's unlikely to need maintenance (like this one), then I think a well-named subroutine beats the module seven days a week. That's the same reason I don't use File::Slurp.

        For my projects, I frequently have several utility functions I use repeatedly. But rather than pull in several different modules each for a different little thing like this, I just keep my MCM::Utils module that contains all of the ones I frequently use. This way, I pull in one module, and have all of them. (Perhaps I ought to clean it up and publish a 'Robojunk' module to cpan module...)

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re^2: Merge 2 strings like a zip
by Athanasius (Archbishop) on Jul 09, 2015 at 03:00 UTC
Re^2: Merge 2 strings like a zip
by tel2 (Pilgrim) on Jul 09, 2015 at 05:47 UTC
    Thanks 1nickt.

    While I didn't think it would be worth the overhead of a module for something so small/simple, I do appreciate your input, and I wasn't aware of that module or function.

      I didn't think it would be worth the overhead of a module for something so small/simple,

      • Personally I don't like the overhead of writing code that I don't need to.
      • Small/simple things have a way of growing.
      • There is more than one way to do it :-)

      Remember: Ne dederis in spiritu molere illegitimi!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found