Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Using Splice with Two Arrays within a loop

by hbm (Hermit)
on Jun 10, 2013 at 15:47 UTC ( #1038103=note: print w/ replies, xml ) Need Help??


in reply to Using Splice with Two Arrays within a loop

You might find it easier by passing references to your arrays:

#!/usr/bin/perl use strict; use warnings; my @first = qw(Can unlock secret); my @second = qw(you the code?); my @mixed = interleave_words( scalar(@first), \@first, \@second ); print "Result: @mixed\n"; sub interleave_words { my ($count, $first, $second) = @_; my @results; die unless $#$first == $#$second; return map { $$first[$_], $$second[$_] } 0..$count-1; }


Comment on Re: Using Splice with Two Arrays within a loop
Download Code
Re^2: Using Splice with Two Arrays within a loop
by Anonymous Monk on Jun 10, 2013 at 17:32 UTC
    Hi, Sorry to clarify I have an assignment that requires the following to be done: "This program can be made both shorter and faster by using a built-in function named splice that will remove a specified number of elements from the @_ array at once, rather than the one-at-a-time that shift does." As you can see from my output I am doing something wrong. Can someone help me out. I need to use splice to resolve this. Thanks
      You're supposed to split the argument list into two with splice before the loop.
Re^2: Using Splice with Two Arrays within a loop
by AnomalousMonk (Monsignor) on Jun 10, 2013 at 18:09 UTC
    You might find it easier by passing references to your arrays ...

    But if you're going to do that, you might as well go all the way and dispense with the count argument altogether as mentioned by davido. In fact, this might actually be a good use for prototyping. Of course, if you use a prototype, you just end up with a version of  mesh() that is specialized for two arguments (easily generalized) and does array-size checking, which  mesh() doesn't. Still...

    >perl -wMstrict -le "use List::MoreUtils qw(mesh); use Data::Dump; ;; my @first = qw(Can unlock secret); my @second = qw(you the code?); ;; sub interleave_words (\@\@); ;; my @mixed = interleave_words(@first, @second); print qq{Result: '@mixed'}; ;; sub interleave_words (\@\@) { die 'array sizes differ' unless $#{$_[0]} == $#{$_[1]}; goto &mesh; } ;; my @ra = qw(a b c); my @rb = qw(1 2 ); dd [ mesh @ra, @rb ]; " Result: 'Can you unlock the secret code?' ["a", 1, "b", 2, "c", undef]

    (Note for anonymous OPer: Don't try this at school unless you really understand what's going on!)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-07-11 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (235 votes), past polls