ro ra
ro ra

Reputation: 419

particular key value from a hash

I am interested only to know the value of key "DelayedAutoStart" and expect this code to work, but it print much more information. Can someone tell me what is wrong here?

   use Win32::Registry;
   use Data::Dumper;
   $p = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion";
   $p = "SYSTEM\\CurrentControlSet\\Services\\sppsvc";
   $main::HKEY_LOCAL_MACHINE->Open($p, $CurrVer) || 
    die "Open: $!";
   $CurrVer->GetValues(\%vals); 
   #print Dumper(\%vals);

   foreach $k (keys %vals) {
   $key = $vals{$k};
   if ($$key[0] == "DelayedAutoStart")
   {
   print "$$key[0] = $$key[2]\n"; 
   }
}

RESULT:

ServiceSidType = 1
ErrorControl = 1
LaunchProtected = 1
DisplayName = @%SystemRoot%\system32\sppsvc.exe,-101
Start = 2
ImagePath = %SystemRoot%\system32\sppsvc.exe
Description = @%SystemRoot%\system32\sppsvc.exe,-100
DelayedAutoStart = 1
ObjectName = NT AUTHORITY\NetworkService
RequiredPrivileges = SeAuditPrivilege SeChangeNotifyPrivilege         
SeCreateGlobalPrivilege SeImpersonatePrivilege
FailureActions = ÇQ☺         ♥   ¶   ☺   └È☺ ☺   Óô♦︎
DependOnService = RpcSs
Type = 16

Upvotes: 1

Views: 99

Answers (1)

Dave Cross
Dave Cross

Reputation: 69314

Please add use strict and use warnings to your code. They will give you an error telling you that you're using the wrong kind of comparison operator. The == equality operator is for comparing numbers not strings. You need eq which does a string comparison.

Also, you're confusing matters rather by storing a hash value in a variable called $key and generally making things far more complicated than they need to be!

foreach my $key (keys %vals) {

  if ($key eq "DelayedAutoStart")
  {
    print "$key = $vals{$key}[2]\n";
  }
}

But, of course, you can just look up the value in the hash directly. No need to iterate over the keys. That's pretty much the point of using a hash :-)

my $key = 'DelayedAutoStart';
if (exists $vals{$key}) {
  print "$vals{$key} = $vals{$key}[2]\n";
}

Upvotes: 5

Related Questions