SPUG: Reference to a hash

Martin, Asa asa.martin at attws.com
Fri Mar 22 11:51:28 CST 2002


I agree that using "my %hash" would be the preferred method. I use strict as
well. However, in my case I couldn't create a new %hash variable on each
iteration through the loop because %hash was not completed for several
iteration through the loop. 

Not to bore everyone, but he's a sample record of what I was parsing:

{
        ObjectName=name;
        ClassName=1.3.6.1.4.1.9.1.45;
        SysDetails=Cisco Internetwork Operating System Software 
IOS (tm) RSP Software (RSP-JSV-M), Version 12.0(7)T,  RELEASE SOFTWARE (fc2)
Copyright (c) 1986-1999 by cisco Systems, Inc.
Compiled Mon 06-Dec-99 19:40 by phanguye;
        NetAddress=xxx.xxx.xxx.xxx;
        NetProtocol=0;
        Type=0;
        AccessString=*****;
        LayoutHint=0;
        Action=0;
        Parents={
                        __item__=xxx.xxx.xxx.xxx;
                        __item__=xxx.xxx.xxx.xxx;
                        __item__=xxx.xxx.xxx.xxx;
                        __item__=xxx.xxx.xxx.xxx;
                        __item__=xxx.xxx.xxx.xxx;
                        __item__=xxx.xxx.xxx.xxx;
                        __item__=xxx.xxx.xxx.xxx;
                        __item__=xxx.xxx.xxx.xxx;
                        __item__=xxx.xxx.xxx.xxx;
                        __item__=xxx.xxx.xxx.xxx;
        };
        SnmpStatus=HasAccess;
        sysDescr=Cisco Internetwork Operating System Software 
IOS (tm) RSP Software (RSP-JSV-M), Version 12.0(7)T,  RELEASE SOFTWARE (fc2)
Copyright (c) 1986-1999 by cisco Systems, Inc.
Compiled Mon 06-Dec-99 19:40 by phanguye;
        sysLocation=Location string;
        sysContact=Person and phone number;
        sysName=name;
        InapNeLifeCounter=1;
        Locator=1.0.14.0.0.12.1;
};

As I went through the loop I had to set "flags" to know where I was in
processing. The %hash was for the Parents field. It wasn't finished until I
hit '};'. Then I would assign it to %HoH{$recordnum}{Parents}. And since all
the keys of %hash were __item__ I also had to create a small subroutine that
made the key unique. What joys. In addition, some of the fields, like
sysDecr above split over multiple lines. It was kind of challenging. But
I've got it figured out now. 

Thanks again for everyone's input.

Asa

-----Original Message-----
From: James Moore [mailto:james at banshee.com]
Sent: Friday, March 22, 2002 9:36 AM
To: spug-list at pm.org
Subject: RE: SPUG: Reference to a hash


I find that when I'm using the kind of construction Jim describes it's
because I'm planning on doing some processing to the hash after creating
it:

      while (my $line = <STDIN>) {
        %hash = my_func($line);
        ...
        $hash{foo} = $bar;
        $things{$key} = \%hash;
      }

Rather than the anonymous hash construction using {}, I usually make the
hash a 'my' variable:

      while (my $line = <STDIN>) {
        my %hash = my_func($line);
        ...
        $hash{foo} = $bar;
        $things{$key} = \%hash;
      }

Doing it this way I find is slightly clearer than using
$things{$key}{foo} = $bar, plus it involves slightly less typing.  It's
really only an issue for slightly more complex operations.

I suspect that you get to this point naturally if you're using 'use
strict.'
Strict is your friend - don't attempt to write non-trivial perl code
without it.   

     ------------------------------------------------------------
                            James M. Moore
                          james at banshee.com
              Banshee Software: Web software development
                    Open Source / .NET / Embedded



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org





More information about the spug-list mailing list