Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Push,pop, and splice!

by mtmcc (Hermit)
on Mar 18, 2014 at 16:56 UTC ( [id://1078812]=note: print w/replies, xml ) Need Help??


in reply to Push,pop, and splice!

Firstly, I agree with toolic above, that hashes are likely to be much better for what you are trying to do.

Second, have a look at splice for the splice syntax. Rather than using the name of the item you want to splice, you should provide the index, and number of items. Something like this:

#!/usr/bin/perl use warnings; use strict; depo(); sub depo { my @storage = qw(knife wand bow); my @inventory = qw(axe sword shovel); my $num = 1; my $choice; my $item; sleep (1); print "Which item would you like to deposit?\n", "$num. Nevermind\n"; foreach my $inv (map(ucfirst, @inventory)){ $num++; print "$num. $inv\n"; } $choice = lc(<STDIN>); chomp $choice; my $index = 'N'; for (my $x = 0; $x <= $#inventory; $x+= 1) { if ($inventory[$x] eq "$choice") { $index = $x; $item = $inventory[$x]; } } if ($index eq 'N') { print "$choice is not available.\n\n"; return; } $item = splice (@inventory, $index, 1); push(@storage,$item); print "You have stored a \"$item\".\n\n"; print STDERR "STORAGE: @storage\nINVENTORY: @inventory\n\n"; }

I hope that helps a little.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-19 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found