ITNW 2310 - PERL
Allan Kochis,Adjunct Professor - CIT

Hashes


  1. Hash functions
    1. keys
      returns a list of the keys in random order.

    2. values
      returns a list of the values in random order.

    3. each
      returns a key value pair

    4. delete
      remove an item from the hash based on key.

    5. exists
      check if a key exists in the hash.

    6. reverse
      on a hash reverses the key value pairs

    7. example
      PerlOutput
      #!/usr/bin/perl
      print "________________________\n";
      system("cat pl5.dat");
      print "________________________\n";
      
      open(IN,"<pl5.dat") or die;
      
      while() {
         chomp;
         ($a,$b)=split;
         $work{$a}=$b;
      }
      
      print "\nKeys are: ";
      print join(":",(keys %work))."\n";
      
      print "\nValues are: ";
      print join(":",(values %work))."\n";
      
      print "\nKey => Value pairs\n";
      while(($a,$b)=each %work) {
        print $a."=>".$b."\n";
      }
      
      delete $work{"red"};
      
      if(exists ($work{"red"}))
      {
        print "\nred here\n";
      } else {
        print "\nred gone\n";
      }
      
      %werk=reverse %work;
      print "\nKey => Value pairs\n";
      while(($a,$b)=each %werk) {
        print $a."=>".$b."\n";
      }
      
      

      ________________________
      red apple
      blue berry
      green pear
      orange orange
      purple grape
      ________________________
      
      Keys are: green:blue:purple:orange:red
      
      Values are: pear:berry:grape:orange:apple
      
      Key => Value pairs
      green=>pear
      blue=>berry
      purple=>grape
      orange=>orange
      red=>apple
      
      red gone
      
      Key => Value pairs
      orange=>orange
      berry=>blue
      pear=>green
      grape=>purple
      

  2. Special hashes
    1. %ENV
      The set of environment variables

    2. %SIG
      Signals mapped to functions

    3. %INC
      The include hash for require. This will be covered in modules.

    4. example
      PerlOutput
      #!/usr/bin/perl
      #
      #
      print "\n----ENV ----\n";
      while(($a,$b)=each %ENV) {
          print $a."=>".$b."\n";
      }
      print "\n----SIG ----\n";
      while(($a,$b)=each %SIG) {
          print $a."=>".$b."\n";
      }
      print "\n----INC ----\n";
      while(($a,$b)=each %INC) {
          print $a."=>".$b."\n";
      }
      

      
      ----ENV ----
      HOME=>/home/kochis
      SSH_ASKPASS=>/usr/libexec/openssh/gnome-ssh-askpass
      REMOTEHOST=>cpe-70-112-26-229.austin.res.rr.com
      LESSOPEN=>|/usr/bin/lesspipe.sh %s
      MAIL=>/var/spool/mail/kochis
      PWD=>/home/kochis/fromoldserver/perly
      LANG=>en_US.UTF-8
      USER=>kochis
      G_BROKEN_FILENAMES=>1
      LOGNAME=>kochis
      HOSTNAME=>CodeRed
      SHLVL=>1
      OLDPWD=>/home/kochis/fromoldserver
      INPUTRC=>/etc/inputrc
      _=>./tab.pl
      PATH=>/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/kochis/bin
      SHELL=>/bin/bash
      HISTSIZE=>1000
      TERM=>ansi
      
      ----SIG ----
      TRAP=>
      NUM42=>
      URG=>
      STOP=>
      NUM39=>
      NUM62=>
      NUM43=>
      USR2=>
      NUM57=>
      NUM56=>
      RTMAX=>
      NUM59=>
      VTALRM=>
      CONT=>
      NUM45=>
      NUM61=>
      TERM=>
      NUM44=>
      NUM36=>
      NUM32=>
      BUS=>
      NUM40=>
      NUM51=>
      IOT=>
      STKFLT=>
      NUM41=>
      KILL=>
      QUIT=>
      NUM37=>
      NUM50=>
      ABRT=>
      NUM48=>
      CLD=>
      NUM34=>
      NUM35=>
      NUM38=>
      TTOU=>
      IO=>
      TSTP=>
      PROF=>
      NUM53=>
      NUM58=>
      SEGV=>
      RTMIN=>
      POLL=>
      PIPE=>
      SYS=>
      NUM46=>
      PWR=>
      CHLD=>
      HUP=>
      FPE=>IGNORE
      NUM54=>
      XCPU=>
      TTIN=>
      NUM52=>
      NUM55=>
      XFSZ=>
      INT=>
      NUM49=>
      UNUSED=>
      WINCH=>
      USR1=>
      ILL=>
      ALRM=>
      NUM47=>
      NUM60=>
      
      ----INC ----
      

© Allan Kochis Last revision 9/21/2005