in reply to Push,pop, and splice!

An array makes a poor database because searches for individual items (in an unsorted array) will be linear searches, and inserts/deletions anywhere other than the first or last element will also be linear. A hash would get you constant time searches insertions, and deletions. Though because a hash's keys are unique, if there's the possibility of having more than one axe, for example, you would need to store a hash-of-arrays to accommodate them.

But this exercise is just for amusement and learning. So here's one way you might do it. I used the IO::Prompt::Hooked module to facilitate the prompting / taking input logic. You could implement it by hand if you wanted, and it wouldn't be all that hard. The module just provides a little sugar.

I've got a couple of data validity checking steps in this solution. Both of them (the ones that include die) should be unreachable. In this trivial code they're not really useful. However, in non-trivial code, it's often worthwhile to complain loudly when something unexpected happens.

use strict; use warnings; use IO::Prompt::Hooked 'prompt'; my @storage = qw( knife wand bow ); my @inventory = qw( axe sword shovel ); while(@inventory) { print "\nYour inventory contains: ( @inventory )\n"; print "Your storage contains: ( @storage )\n"; my $wanted = prompt( message => 'Which item would you like to deposit? (blank to quit) +:', validate => sub { my $i = lc shift; return 1 if ! length $i; return grep { $i eq $_ } @inventory }, error => "Please input a valid answer: ( @inventory ).\n" ); last unless $wanted; my( $item_index ) = grep { $inventory[$_] eq $wanted } 0 .. $#invent +ory; die "Couldn't find $wanted\n" unless defined $item_index; # Impossible. push @storage, splice @inventory, $item_index, 1; die "push/splice failure with <$wanted>.\n" if $storage[-1] ne $wanted; # Impossible. } print <<"EOD"; Now inventory and storage respectively contain: ( @inventory ) ( @storage ) EOD

Here is a sample run:

$ ./mytest.pl Your inventory contains: ( axe sword shovel ) Your storage contains: ( knife wand bow ) Which item would you like to deposit? (blank to quit): swwrd Please input a valid answer: ( axe sword shovel ). Which item would you like to deposit? (blank to quit): sword Your inventory contains: ( axe shovel ) Your storage contains: ( knife wand bow sword ) Which item would you like to deposit? (blank to quit): axe Your inventory contains: ( shovel ) Your storage contains: ( knife wand bow sword axe ) Which item would you like to deposit? (blank to quit): shovel Now inventory and storage respectively contain: ( ) ( knife wand bow sword axe shovel )

Dave