>>>>> "PP" == Philip Potter <philip.g.pot...@gmail.com> writes:

  PP> I found this code example on Stack Overflow to prettyprint a hash:
  PP> (link:
  PP> 
http://stackoverflow.com/questions/2431032/how-to-print-a-key-value-using-the-perl-hases/2431139#2431139
  PP> )

you shouldn't be learning perl from that site!

  PP> my %hash = (2009 => 9, 2010 => 10);

see what happens when you have 3 or more pairs in that hash!

  PP> print join (" => ", each %hash), "\n" while <keys %hash>;

try doing a perl one liner with -MO=Deparse and see what perl parses
that as.

  PP> My question is: what is the angle bracket operator doing? I know its
  PP> use as <FILE>, <$file>, and <*.glob>, but I've never seen it used like
  PP> this, and I can't see anything in perlop#I/O Operators which would
  PP> help. This code works on perl 5.8.8 so it's been a feature for some
  PP> time, so it would surprise me if it's not documented anywhere, but I
  PP> just can't find it.

it is just a glob which expands to the files that match or just the
tokens it is passed if no expansions happen. so that is doing a while
loop for 2 iterations (hence my comment about 3 or more pairs in the
hash). it is just a very stupid and broken example posted on that
site. even if the <> did something to do with hashes, why would you loop
over the keys AND also call each? the proper way to simple loop over a
hash is to use each and get both the key and value at one time:

        while( my( $key, $val ) = each %hash ) {

                print "$key => $val\n" ;
        }

that loops properly and for all the key/value pairs in the hash.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to