----- Original Message ----- From: "Wiggins d Anconia" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Thursday, September 16, 2004 11:30 AM Subject: Re: HoH not building as expected
> > 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. That is what I though, untill someone corrected me.... > > > 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. yes, this is actualy not a 100% of my code, just a small snipet to get me through the hurdle... > > > 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, I do apologize, you are correct. I am looking for HoHoH. I need this structure so I can have fast access to data, instead of itterating through array when I need to compare with other data. > > 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. yes, once I can get my structure deeper > > > 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); ...though I can understand the problem beter by constructing the irretations. > > Suggested reading: > > perldoc perldsc > perldoc perllol > perldoc perlreftut > perldoc perlref > > HTH, Thankz WiG, if you can give it another shot with comments would be great. > > 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> > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>