List Info

Thread: printing hashes of arrays




printing hashes of arrays
user name
2007-01-02 05:25:54
Hi there list,

I am having a little difficulty understanding how to print
all the 
elements of arrays that are encompassed in a hash.

I am reading a CSV file into a hash of hashes styled record
in the sub 
read_datafile, and the printing is done in the sub called 
print_all_records.  In print_all_records I am having 
difficulty 
printing the output of all the elements of "{
$record-> }".  
it appears that only the first element prints. The variable 
scalar { 
$record-> } clearly shows there are more than 1
element for 
some of the arrays.

Below are the subroutines,  the output from the sub
print_all_records, 
and the CSV data file. 

Any assistance is appreciated in advance.

---- perl subs ----
sub read_datafile
{
    open(DATA,$input_file) || die "unable to open
$input_file $! n";
    while( <DATA> )
    {
        chomp;
        $_ =~  s/s+$//;   # Need this to handle DOS format
txt files
        vals = split(/,/);
        $sitename          = $vals[0];
        $hostname          = $vals[1];
        $loopback          = $vals[2];
        $neighbor          = $vals[3];
        $local_interface   = $vals[4];
        $remote_interface  = $vals[5];
        $local_ip          = $vals[6];
        $neighbor_ip       = $vals[7];
        $value = $#vals + 1;
#
#printf( STDERR 
">>%s:%s:%s:%s<<n",$sitename,$hostnam
e,$loopback,$neighbor,$local
_interface,$remote_interface,$local_ip,$neighbor_ip );
        if ( $value eq 8 )
        {
            $sitename =~ s/^s+//;
            $hostname =~ s/^s+//;
            $hostname =~ tr/A-Z/a-z/;
            $loopback =~ s/^s+//;
            $neighbor =~ s/^s+//;
            $neighbor =~ tr/a-z/A-Z/;
            $local_interface =~ s/^s+//;
            $remote_interface =~ s/^s+//;
#           $local_ip = &parse_ip($local_ip);
#           $neighbor_ip = &parse_ip($neighbor_ip);
            $local_ip =~ s/^s+//;
            $neighbor_ip =~ s/^s+//;

#    
printf("%4d,%s:%s:%7.2f:%7.2fn",$value,$sitename,
$hostname,$loopback,$neighb
or,$local_interface,$remote_interface,$local_ip,$neighbor_ip
);

#           print "hostname: $hostnamen";
#           printf "byhostname: <<%s>>
n", keys %byhostname;
#           printf "byhostname: $hostname now has
<<%s>> n",  
$byhostname{$hostna
me}->;

            if (! exists
$byhostname{$hostname}-> )
            {
                $record =
                {
                    HOSTNAME         => $hostname,
                    SITENAME         => $sitename,
                    LOOPBACK         => $loopback,
                };
                $byhostname[ $record-> ] =
$record;
            }

            push { $byhostname {$hostname}->
}, $neighbor;
            push { $byhostname
{$hostname}-> }, 
$local_interface
;
            push { $byhostname
{$hostname}-> }, 
$remote_interfa
ce;
            push { $byhostname {$hostname}->
}, $local_ip;
            push { $byhostname
{$hostname}-> }, $neighbor_ip;

            print Dumper($record);

        } else {
            print STDERR "value = ",$value;
            printf(STDERR "*** Parse error at line %d 
;n",$.);
            printf(STDERR "     %sn",$_);
        }
    }
    close DATA;
}


----- sub print_all_records  ------

sub print_all_records
{
    while (($hostname, $record) = each %byhostname)
    {
        printf "%s is hostname
<<%s>>n", $hostname, { 
$record-> };
        printf "%s is hostname
<<%s>>n", $hostname, scalar { 
$record->
 };
    }
}



---- output from print_all_records ----

k7-ml3ag01 is hostname <<1.2.3.245/30>>
k7-ml3ag01 is hostname <<1>>
cs5-f10ag01 is hostname <<1.2.3.153/30>>
cs5-f10ag01 is hostname <<7>>
k7-ss2ag01 is hostname <<1.2.3.233/30>>
k7-ss2ag01 is hostname <<2>>
gl11-sanag02 is hostname <<1.2.3.225/30>>
gl11-sanag02 is hostname <<1>>
bl5-jimag01 is hostname <<1.2.3.209/30>>
bl5-jimag01 is hostname <<2>>
k7-dyhag01 is hostname <<1.2.3.241/30>>
k7-dyhag01 is hostname <<1>>
cs5-wineag01 is hostname <<1.2.3.189/30>>
cs5-wineag01 is hostname <<4>>
gl10-blaag01 is hostname <<1.2.3.177/30>>
gl10-blaag01 is hostname <<2>>
cs5-sanwag01 is hostname <<1.2.3.193/30>>
cs5-sanwag01 is hostname <<4>>
cs5-rouag01 is hostname <<1.2.3.205/30>>
cs5-rouag01 is hostname <<3>>
cs5-whxag01 is hostname <<1.2.3.157/30>>
cs5-whxag01 is hostname <<4>>
k7-psnag01 is hostname <<1.2.4.9/30>>
k7-psnag01 is hostname <<1>>
cs5-ftl2ag01 is hostname <<1.2.3.185/30>>
cs5-ftl2ag01 is hostname <<4>>
k7-blahag01 is hostname <<1.2.4.21/30>>
k7-blahag01 is hostname <<1>>
cs5-core01 is hostname <<1.2.3.153/30>>
cs5-core01 is hostname <<5>>
k7-jbag01 is hostname <<1.2.4.13/30>>
k7-jbag01 is hostname <<2>>


------ CSV datafile -----

Ocean 
59,cs5-f10ag01,1.2.3.104/32,AA1-UPAG01,ge-0/0/0,ge-0/0/0,1.2
.3.153/30,1.2.3.154/30,,,
Ocean 
59,cs5-f10ag01,1.2.3.104/32,AA1-WHXAG01,ge-0/0/1,ge-0/0/0,1.
2.3.157/30,1.2.3.158/30,,,
Ocean 
59,cs5-f10ag01,1.2.3.104/32,AA1-WINEAG01,ge-0/0/3,ge-0/0/1,1
.2.3.161/30,1.2.3.162/30,,,
Ocean 
59,cs5-f10ag01,1.2.3.104/32,AA1-SUNAG01,ge-0/2/0,ge-0/0/1,1.
2.3.165/30,1.2.3.166/30,,,
Ocean 
59,cs5-f10ag01,1.2.3.104/32,AA1-SFO2AG01,ge-0/2/1,ge-0/0/1,1
.2.3.169/30,1.2.3.170/30,,,
Ocean 
59,cs5-f10ag01,1.2.3.104/32,AA1-JUMDWAG01,ge-0/2/2,ge-0/2/0,
1.2.3.173/30,1.2.3.174/30,,,
Ocean 
59,cs5-f10ag01,1.2.3.104/32,GL10-GLOBAG01,ge-0/2/3,ge-0/0/0,
1.2.3.177/30,1.2.3.178/30,,,
UP,cs5-core01,1.2.3.105/32,cs5-f10ag01,ge-0/0/0,ge-0/0/0,1.2
.3.153/30,1.2.3.154/30,,,
UP,cs5-core01,1.2.3.105/32,AA1-WHXAG01,ge-0/0/1,ge-0/0/1,1.2
.3.181/30,1.2.3.182/30,,,
UP,cs5-core01,1.2.3.105/32,AA1-SFO2AG01,ge-0/0/2,ge-0/0/0,1.
2.3.185/30,1.2.3.186/30,,,
UP,cs5-core01,1.2.3.105/32,cs5-wineag01,ge-0/0/3,ge-0/0/0,1.
2.3.189/30,1.2.3.190/30,,,
UP,cs5-core01,1.2.3.105/32,AA1-JUMDWAG01,ge-0/2/0,ge-0/0/0,1
.2.3.193/30,1.2.3.194/30,,,
GLOB,gl10-blaag01,1.2.3.106/32,cs5-f10ag01,ge-0/0/0,ge-0/2/3
,1.2.3.177/30,1.2.3.178/30
GLOB,gl10-blaag01,1.2.3.106/32,AA1-JUMDWAG01,ge-0/0/1,ge-0/0
/0,1.2.3.197/30,1.2.3.198/30
NPA,cs5-rouag01,1.2.3.107/32,AA1-SFO2AG01,ge-0/0/0,ge-0/0/3,
1.2.3.205/30,1.2.3.206/30
NPA,cs5-rouag01,1.2.3.107/32,bl5-jimag01,ge-0/0/1,ge-0/0/0,1
.2.3.209/30,1.2.3.210/30
NPA,cs5-rouag01,1.2.3.107/32,AA1-JUMDWAG01,ge-0/2/0,ge-0/0/1
,1.2.3.213/30,1.2.3.214/30
SFO2,cs5-ftl2ag01,1.2.3.108/32,AA1-UPAG01,ge-0/0/0,ge-0/0/2,
1.2.3.185/30,1.2.3.186/30
SFO2,cs5-ftl2ag01,1.2.3.108/32,cs5-f10ag01,ge-0/0/1,ge-0/2/1
,1.2.3.169/30,1.2.3.170/30
SFO2,cs5-ftl2ag01,1.2.3.108/32,AA1-WHXAG01,ge-0/0/2,ge-0/2/0
,1.2.3.217/30,1.2.3.218/30
SFO2,cs5-ftl2ag01,1.2.3.108/32,cs5-rouag01,ge-0/0/3,ge-0/0/0
,1.2.3.205/30,1.2.3.206/30
SUN,bl5-jimag01,1.2.3.109/32,cs5-rouag01,ge-0/0/0,ge-0/0/1,1
.2.3.209/30,1.2.3.210/30
SUN,bl5-jimag01,1.2.3.109/32,cs5-f10ag01,ge-0/0/1,ge-0/2/0,1
.2.3.165/30,1.2.3.166/30
JUM1,cs5-sanwag01,1.2.3.110/32,AA1-UPAG01,ge-0/0/0,ge-0/2/0,
1.2.3.193/30,1.2.3.194/30
JUM1,cs5-sanwag01,1.2.3.110/32,AA1-NPAAG01,ge-0/0/1,ge-0/2/0
,1.2.3.213/30,1.2.3.214/30
JUM1,cs5-sanwag01,1.2.3.110/32,AA1-MIA36AG01,ge-0/2/0,ge-0/2
/2,1.2.3.173/30,1.2.3.174/30
JUM1,cs5-sanwag01,1.2.3.110/32,AA1-WINEAG01,ge-0/2/1,ge-0/0/
3,1.2.3.221/30,1.2.3.222/30
JUM2,gl11-sanag02,1.2.3.111/32,GL10-GLOBAG01,ge-0/0/0,ge-0/0
/1,1.2.3.225/30,1.2.3.226/30
Dulles,cs5-wineag01,1.2.3.112/32,AA1-UPAG01,ge-0/0/0,ge-0/0/
3,1.2.3.189/30,1.2.3.190/30
Dulles,cs5-wineag01,1.2.3.112/32,AA1-MIA36AG01,ge-0/0/1,ge-0
/0/3,1.2.3.161/30,1.2.3.162/30
Dulles,cs5-wineag01,1.2.3.112/32,AA1-WHAG01,ge-0/0/2,ge-0/2/
1,1.2.3.229/30,1.2.3.230/30
Dulles,cs5-wineag01,1.2.3.112/32,AA1-JUMDWAG01,ge-0/0/3,ge-0
/2/1,1.2.3.221/30,1.2.3.222/30
WH,cs5-whxag01,1.2.3.113/32,AA1-F10AG01,ge-0/0/0,ge-0/0/1,1.
2.3.157/30,1.2.3.158/30
WH,cs5-whxag01,1.2.3.113/32,AA1-UPAG01,ge-0/0/1,ge-0/0/1,1.2
.3.181/30,1.2.3.182/30
WH,cs5-whxag01,1.2.3.113/32,AA1-SFO2AG01,ge-0/2/0,ge-0/0/2,1
.2.3.217/30,1.2.3.218/30
WH,cs5-whxag01,1.2.3.113/32,AA1-WINEAG01,ge-0/2/1,ge-0/0/2,1
.2.3.229/30,1.2.3.230/30
Austrin,K7-SS2AG01,1.2.3.114/32,AA1-WINEAG01,ge-0/0/0,ge-0/1
/0,1.2.3.233/30,1.2.3.234/30
Austrin,K7-SS2AG01,1.2.3.114/32,AA1-JUMDWAG01,ge-0/0/1,ge-0/
2/2,1.2.3.237/30,1.2.3.238/30
HalfMoon,K7-DYHAG01,1.2.3.115/32,AA1-SFO2AG01,ge-0/0/0,ge-0/
1/0,1.2.3.241/30,1.2.3.242/30
Melbourne,K7-ML3AG01,1.2.3.116/32,AA1-SFO2AG01,ge-0/0/0,ge-0
/1/1,1.2.3.245/30,1.2.3.246/30
Berkeley,K7-PSNAG01,1.2.3.118/32,K7-JBAG01,ge-0/0/0,ge-0/0/2
,1.2.4.9/30,1.2.4.10/30
Beach,K7-JBAG01,1.2.3.119/32,AA1-JUMDWAG01,ge-0/0/0,ge-0/2/3
,1.2.4.13/30,1.2.4.14/30
Beach,K7-JBAG01,1.2.3.119/32,AA1-NPAAG01,ge-0/0/1,ge-0/2/1,1
.2.4.17/30,1.2.4.18/30
BLAH,K7-BLAHAG01,1.2.3.120/32,AA1-SFO2AG01,ge-0/0/0,ge-0/1/2
,1.2.4.21/30,1.2.4.22/30



Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
printing hashes of arrays
user name
2007-01-04 17:53:13
>>>>> "Noah" == Noah 
<admin2enabled.com> writes:

Noah> I am reading a CSV file into a hash of hashes
styled record in the sub 
Noah> read_datafile, and the printing is done in the sub
called 
Noah> print_all_records.  In print_all_records I am
having  difficulty 
Noah> printing the output of all the elements of
"{ $record-> }".  
Noah> it appears that only the first element prints. The
variable  scalar { 
Noah> $record-> } clearly shows there are
more than 1 element for 
Noah> some of the arrays.

I'm a little groggy yet this morning, but I notice two
things.  You're not
using "my" to introduce your variables, meaning
that they're all either
outer-scoped lexical or (ugh) package variables.  If that's
the case,
then this is troubleseome for these lines:

Noah>             if (! exists
$byhostname{$hostname}-> )
Noah>             {
Noah>                 $record =
Noah>                 {
Noah>                     HOSTNAME         =>
$hostname,
Noah>                     SITENAME         =>
$sitename,
Noah>                     LOOPBACK         =>
$loopback,
Noah>                 };
Noah>                 $byhostname[ $record->
] = $record;
Noah>             }

This will keep using the same $record value repeatedly (I
think)
for each new entry, which will definitely mess up your day.

Please learn to "use strict" and use new local
variables when building up
complex data structures.  See my column on "Deep
Copy" at
<http://www.stonehenge.com/merlyn/UnixReview/col30.html
> for more explanation.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
+1 503 777 0095
<merlynstonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy,
etc. etc.
See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!


Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
printing hashes of arrays
user name
2007-01-04 17:54:44
--- In perl-beginner@yahoogroups.com, Noah <admin2...>
wrote:
>
> Hi there list,
> 
> I am having a little difficulty understanding how to
print all the 
> elements of arrays that are encompassed in a hash.
> 
perldoc perldsc
Data Structures Cookbook
It describes how to create hashes of hashes and then how to
access and
print them. It also does other combinations of arrays and
hashes.

http://aspn.activestat
e.com/ASPN/docs/ActivePerl/5.8/lib/Pod/perldsc.html#hashes_o
f_hashes



Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
printing hashes of arrays
user name
2007-01-05 20:34:11
--- In perl-beginner@yahoogroups.com, Noah <admin2...>
wrote:
>
> Hi there list,
> 
> I am having a little difficulty understanding how to
print all the 
> elements of arrays that are encompassed in a hash.
> 
> I am reading a CSV file into a hash of hashes styled
record in the 
sub 
> read_datafile, and the printing is done in the sub
called 
> print_all_records.  In print_all_records I am having 
difficulty 
> printing the output of all the elements of "{
$record->
 }".  
> it appears that only the first element prints. The
variable  
scalar { 
> $record-> } clearly shows there are more
than 1 element 
for 
> some of the arrays.
> 
> Below are the subroutines,


[snip]

> 
> ----- sub print_all_records  ------
> 
> sub print_all_records
> {
>     while (($hostname, $record) = each %byhostname)
>     {
>         printf "%s is hostname
<<%s>>n", $hostname, { 
> $record-> };


This is one way to give you all records.


printf "%s is hostname <<{$record->
}>>n", $hostname;




>         printf "%s is hostname
<<%s>>n", $hostname, scalar { 
> $record->
>  };
>     }
> }

Chris




Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )