> Hi All,
>       I am trying to build a HoH, who's keys will be referances to
products. However it seems if $e [ vendor value] is the same, the hash
get's recreated instead of appending a product to already existing hash.
Any ideas would be apreciated,
> 
> #!PERl -w
> use warnings;

You don't need both -w and 'warnings'. -w will turn on warnings for the
complete program (including modules), the warnings pragma turns on
warnings for only the lexical scope, in this case the file.

> use strict;
> use String::Scanf;
> 
> my (@te,$a, $b, $c, $d, $e,%RATING,$CPU_ATTR,$debug,$attrib,$key);
> 

By declaring all your variables like this at the top you have cut out
half of what the strictures will help you with, generally it is
considered bad form. Also note the variables name such as $a, $b, etc.
are also considered special and should not be used outside of the
context of a C<sort> subroutine.

> my $s1 = String::Scanf->new("%s %s %s %s %s"); #define string template
> 
> 
> @te = (   "CREATE       CPU-MODEL UNKNOWN_CPU VENDOR Unknown, GENERIC_CPU
VENDOR SUN\n" ,
>         "CREATE         CPU-MODEL model1 VENDOR Unknown, GENERIC_CPU VENDOR
Unknown\n" );
> 
> s/,|\t//g for @te;
> 
> 
> foreach ( @te ){
> 
> print "LINE=$_" if $debug;
> 
>  
> ($a, $b, $c, $d, $e) = $s1->sscanf();
> 
> if( $a eq 'CREATE' ){
>         $CPU_ATTR= {};
>         $RATING{"$e"} = $CPU_ATTR;# main key is Vendor hashref to CPU_ATTR

A hash contains only one value for each key, in this loop you are
continually overwriting that value so that only the last will exist.
Your structure is incorrect, instead of a HoH you want a HoAoH. You
should also not bother quoting $e, so the above becomes,

push @{$RATING{$e}}, $CPU_ATTR;

>         $CPU_ATTR->{$b}=$c; #processor [EMAIL PROTECTED]
>         $CPU_ATTR->{$d}=$e; #processor [EMAIL PROTECTED]
> }
> 
> }
> 
> while ( my ($key, $value) = each(%RATING) ) {
>         print "$key =>:\n";

In here $value will itself be an Array reference so you will have to put
the following loop inside of a loop of its own.

>         for $attrib ( keys %$value ){
>                 print "$attrib :";print $value->{$attrib} . "\n";
>         }
> print "\n\n";
> }
> 
> I expact the output to be 
> 
> Unknown =>:
> VENDOR :Unknown
> CPU-MODEL :model1
> Unknown =>:
> VENDOR :Unknown
> CPU-MODEL :Unknown  
> 

It is very easy to check the actual structure with the Data::Dumper
module, in your case you might check with:

use Data::Dumper;
print Dumper(\%RATING);

Suggested reading:

perldoc perldsc
perldoc perllol
perldoc perlreftut
perldoc perlref

HTH,

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to