Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Using Splice with Two Arrays within a loop

by moritz (Cardinal)
on Jun 10, 2013 at 15:45 UTC ( #1038102=note: print w/ replies, xml ) Need Help??


in reply to Using Splice with Two Arrays within a loop

I don't understand your question. First you say I need to remove a specified number of elements from the @_ array at once rather than the one-at-a-time that shift does, which your code does with splice.

But then you give it the same input as before, and expect the same outcome, even though you changed the code. Why did you even change the code if you don't want it to change? And how do you expect you will ever get more than two elements into @merge_list, when all you do is to create a new variable and assign to elements to it?


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:03 UTC
    Sorry for not being clear the issue is that the first part of the code is the original code that I have to modify to work using Splice. Since I am a noobie with Perl the bottom code is my attempt which is not working. I need to use splice to make this work as part of my assignment.

      Here is the splice version. Make sure you understand what's going on before you submit to whomever... (there is a twist...)

      #!/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 = shift; my @results = splice @_, $count; foreach my $index ( 0 .. $count-1 ) { splice @results, 2*$index, 0, shift; } return @results; }
Re^2: Using Splice with Two Arrays within a loop
by Anonymous Monk on Jun 10, 2013 at 17:29 UTC
    Applogies: I have a assignment as follows: "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." Using the existing code I need to change it to use splice and have the same exact output. As you can see it is not working. I need some help in this. Thanks

      Be very careful when you ask for "short" code on this website, you might get what you ask for ... ;) like this (meeting your requirement to use splice):

      sub interleave_words { my @results = splice @_, shift; splice @results, 2*$_, 0, shift for 0..@results-1; return @results; }
        Hi thanks for the process however I noticed that the die statement is no longer in the code. I need to include the statement. How do I include that without it printing the die statement instead of the answer I wanted which was Result: Can you unlock the secret code?
        Thanks for the help. Question I still require to keep the die statement in the code however not sure how I put it in without the die statement printing out instead of the correct answer. Thanks for any input

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-12-22 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (121 votes), past polls