Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hash does not assemble correctly

Status
Not open for further replies.

nawlej

Programmer
Mar 26, 2004
380
US
I am trying to build a big hash to be exported based on the keys and values from another hash, This is the jist of it:
the function takes array @data as input from each value of the keys in %outserv. it then cleans out and strips the data I need and puts it into its own hash, which is returned by the function. The script then pushed the contents of the hash onto the master hash, and after the loop is finished, you end up with %refined. The first time it goes through the loop, it runs without incident, but after that, the output looks as if it is referencinv the first one I test. WHen I do a Dumper on it, the subsequent values for $refined{$key} just say: $VAR1->{'server1'}[0] The key variable outputs correctly to the hash, but the values for it after the first do not. I suspect it to be a referencing problem. Here is my codefor the while loop:

Code:
  while (($key,@data) =  (each %outserv)){
           %manout = &man_data(@data);
           #push the entire cluster check on the final hash
           $href = \%manout;
           push(@{$refined{$key}},$href);
  }

 print &Dumper(\%refined)." \n";

Here is sub man_data:

Code:
 sub man_data{
  my ($clusterdata,$identifier,$servicegroup,$clustnode,$junk1,$junk2,$sysstate,$nodestatus,$frozen,$category);
  my (%stripped,%A,%B,%C,$hashref);
  my @incoming = split(/\n/,$_[0][0]);

  #manipulate the data, store it in hash.
  foreach $clusterdata (@incoming){
          #strip out and eliminate junk
          chomp $clusterdata;
          $clusterdata =~ s/^\s+//;
          #grab group states and put them into their individual hashes
          if($clusterdata =~ /^A/){
             ($identifier,$clustnode,$sysstate,$frozen) = split(/\s+/,$clusterdata);
             $nodestatus = "$sysstate~$frozen";
             push (@{$A{$clustnode}},$nodestatus);

          }elsif($clusterdata =~ /^B/){
             ($identifier,$servicegroup,$clustnode,$junk1,$junk2,$sysstate) = split(/\s+/,$clusterdata);
             $nodestatus = "$clustnode~$sysstate";
             push (@{$B{$servicegroup}},$nodestatus);

          }elsif($clusterdata =~ /^C/){
             ($identifier,$servicegroup,$junk1,$junk2,$clustnode) = split(/\s+/,$clusterdata);
             push (@{$C{$servicegroup}},$clustnode);
          }else{
           next;
                 #Trash It!
          }

  }
  #initialize the hash for pushing all of then subhashes onto it.

  #push it all onto one big hash
  if(%A){
  $hashref = \%A;
  push(@{$stripped{"A"}},$hashref);
  }
  if(%B){
  $hashref = \%B;
  push(@{$stripped{"B"}},$hashref);
  }
  if(%C){
  $hashref = \%C;
  push(@{$stripped{"C"}},$hashref);
  }
  #return the entire hash
  return (%stripped);

 }#END - man_data

Can anyone help?
 
Without looking at your code too closely, I think the problem may be the line
$href = \%manout;
in the first piece of code. Try changing this to
$href = { %manout };
i.e., get rid of the backslash and put %manout inside curlies. See if that does it,
 
I fixed it this morning. I found that the problem was %manout being out of scope. I redeclared it inside of the loop as a my and also took the push out. That seemed to do the trick. I can see where your way could work too, as your taking out the reference and building $href as an anonymous data structure. Thank you for all of your help.

Code:
  while (($key,@data) =  (each %outserv)){

          my %manout = &man_data(@data);

          #put the entire cluster check on the final hash
          $refined{$key} = \%manout;
  }
 
Glad you got it to work.
The reason I suggested what I did is that your code looked very much like one of the wrong examples in perldsc (Data Structures Cookbook), the section "Common Mistakes". My suggestion was based on one of the fixes given there. What you came up with is the other suggested fix. [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top