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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You can consider a hash to be much like an array, except instead of numerical indexes to access individual elements, you use strings as keys.

# Define an array: my @basket = qw(apple banana cherry); # Get an element from the array: # (remember that indexes are 0-based) print "The second kind of fruit in the basket is $basket[1]\n"; # Change an element: $basket[1] = "date"; print "Now it is $basket[1]\n";

The above example shouldn't be unfamiliar. Now, instead of keeping a @basket that tells us what kinds of fruit we have in the basket, let's keep a %basket that can also tell us how much of that kind of fruit we have.

# Define the hash: my %basket = ( apple => 12, banana => 6, cherry => 32, # This final comma is optional, ); # but makes it easier to add more lines in the f +uture. # Get an element from the basket: print "There are $basket{cherry} cherries in the basket.\n"; # Modify elements: $basket{cherry}--; print "Now there are $basket{cherry}.\n"; $basket{banana} *= 2; print "Double Banana Bonus! $basket{banana} bananas in the basket!\n"; $basket{apple} = 10; # Add an element: $basket{date} = 16; # Get all keys in the hash: print "Fruits in my basket: ", join(", ", sort keys %basket), "\n"; # Using a variable as a key: for my $fruit (sort keys %basket) { print "You want a(n) $fruit? I have $basket{$fruit} in my basket.\ +n"; } # The 'each' function: while (my ($fruit, $amount) = each %basket) { print "There are $amount ${fruit}s in my basket.\n"; } # Getting rid of an element: delete $basket{apple}; print "Fruits in my basket: ", join(", ", sort keys %basket), "\n";

That pretty much covers the basics of hashes. Nothing to be afraid of, and quite a useful data type!


In reply to Re^3: Replacing values in an array by muba
in thread Replacing values in an array by tonto

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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2024-09-17 05:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The PerlMonks site front end has:





      Results (22 votes). Check out past polls.

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.