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


In reply to Re: Push,pop, and splice! by davido
in thread Push,pop, and splice! by Jabox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.