Loading ...
Sorry, an error occurred while loading the content.

Re: Hash question

Expand Messages
  • andy_bach@wiwb.uscourts.gov
    ... works, but if this is right, I don t understand why it needs to be where it is. Line 3 - %hash = (); It s an old cargo cult thing - or belts/suspenders,
    Message 1 of 5 , Dec 2, 2008
    • 0 Attachment
      > 1. Why does the first line of the code have to be an empty hash? It
      works, but if this is
      right, I don't understand why it needs to be where it is.
      Line 3 - %hash = ();

      It's an old 'cargo cult' thing - or belts/suspenders, I guess. You're
      assigning an empty list (the parens) to the hash, which would clear any
      existing values. Akin to:
      $name = '';

      The bigger issue here is the lack of lexical safety, if you do:
      my %hash;

      You don't need to worry about cleaning it out, as you're assured it was
      just created. Of course, the name 'hash' isn't very good either. You will
      see:
      my %hash = ();

      it's just extra typing and, maybe, slightly more obvious but entirely
      unnecessary. You really, really need to use lexical safety, in any program
      over ... 5 lines. It should be standard to start your scripts w/:
      use warnings;
      use strict;

      and, on this list, it should be a pretty standard response to any posted
      code - it should, at least, come in strict/warnings clean.

      $name_found = 'N'; #MY 2ND QUESTION IS ABOUT THIS LINE AND WHY IT WORKS
      HERE.
      foreach $name (@newarray)
      {
      if ("$input_name" eq "$name")
      {
      print $name." has the password ".$hash{$name}."\n";
      $name_found = 'Y';
      last;
      }

      } #close foreach
      if ("$name_found" eq 'N')
      {
      print "Sorry, there is no such person found. Please try again. \n";
      }

      This is a little odd - the point of a hash is you can fetch directly by key
      so:
      if ( exists $hash{$input_name} ) {
      print $name." has the password ".$hash{$name}."\n";
      }
      else {
      print "Sorry, there is no such person found. Please try again. \n";
      }

      does the same thing - no need to concat or refetch though:
      if ( my $pwd = $hash{$input_name} ) {
      print "$name has the password $pwd\n";
      }
      else {
      print "Sorry, there is no such person found. Please try again. \n";
      }

      If the assignment succeeds (that is, something 'exists' in %hash at the key
      $input_name) the 'if' carries through.
      -------------------
      Andy Bach
      Systems Mangler
      Internet: andy_bach@...
      Voice: (608) 261-5738 Fax: 264-5932

      If I have seen farther than others, it is because I was standing on the
      shoulders of giants. -- Isaac Newton
      In the sciences, we are now uniquely privileged to sit side by side
      with the giants on whose shoulders we stand. -- Gerald
      Holton
      If I have not seen as far as others, it is because giants were standing
      on my shoulders. -- Hal Abelson
      In computer science, we stand on each other's feet. -- Brian
      K. Reid
    Your message has been successfully submitted and would be delivered to recipients shortly.