Jabox has asked for the wisdom of the Perl Monks concerning the following question:
Good day everyone!
I am trying to practice push and pop so I was making this little project similar to an NPC that stores your items.
The snippet probably looks like something that doesn't exist or make sense because I'm still learning so I apologize and I accept all responses even if they are suppose to offend me.So here is a snippet, will post problem afterwards!
my @storage = qw(knife wand bow); my @inventory = qw( axe sword shovel ); sub depo { sleep (1); print "Which item would you like to deposit?\n", "1. Nevermind\n"; foreach my $inv (map(ucfirst, @inventory)){ print "$num. $inv\n"; $num++; } $choice = lc(<STDIN>); chomp $choice; if ($choice =~ $item) { my $item = splice @inventory, $choice; push(@storage,$item); print "You have stored a \"$item\".\n"; }
SO what I am trying to do is that if I put in "Axe" from my @inventory and it exist, it will take the "axe" from @inventory and add it to @storage so then @storage will equals to knife wand bow axe while @inventory will equals to only sword and shovel.
And if it doesn't exist, it will do
else { print "Please input a valid answer!\n"; sleep (1); &bank; }
Thank you for your time :D!
P.S. there was a grep function before the if($choice =~$item).. thats why the $item was there if you were wondering.. I was experimenting :D!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Push,pop, and splice!
by davido (Cardinal) on Mar 18, 2014 at 17:19 UTC | |
|
Re: Push,pop, and splice!
by toolic (Bishop) on Mar 18, 2014 at 16:37 UTC | |
|
Re: Push,pop, and splice!
by mtmcc (Hermit) on Mar 18, 2014 at 16:56 UTC | |
|
Re: Push,pop, and splice!
by Don Coyote (Hermit) on Mar 18, 2014 at 21:43 UTC |