Solved

PERL - Hash / ForEach / Array - Why can't I get my Hash in PERL to work correctly

Posted on 2011-09-27
5
337 Views
Last Modified: 2012-06-21
I want it to say key => davidr, key => mikes (show all the accountnames in the hash)
It does not work! Please help! I only get a blank hash with this:
$VAR1 = {
          'key' => '
'
        };

use Data::Dumper;
$last = "likus";
$first = "to";
$char = substr( $first, $i , $count );
$accountname = $last . $char;
my %hash;

@accounts = `net user /domain`;
foreach $line (@accounts){
$line =~ s/\\\\.+//g;
$line =~ s/User accounts for//ig;
$line =~ s/(-----)-+//ig;
$line =~ s/The command.+//ig;
$line =~ s/\s+/\n/g;
#Print "$line";
}
#print @accounts;
foreach $line (@accounts){
$hash{"key"}=$line;
#print "$line\n";
}
print Dumper(\%hash);

Open in new window

0
Comment
Question by:_-W-_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 500 total points
ID: 36713894
On line 19: $hash{"key"}=$line;
this doesn't seem right since you keep on overwriting the value associated with the key "key".
Isn't the key supposed to change within that foreach loop?
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 36714361
What jeromee said.

Also, lines 2-5 don't seem directly related to the rest (those vars are never used again).  You could combine the two foreach loops (guessing you split them for debugging.

line 10 looks odd.  Do the lines look okay if you uncomment line 17?  Unless I'm mistaken, line 10 is saying backslash, backslash, any number of any character (so everything on the line after a double backslash will get removed).  Similarly with line 13 - it will remove everything after "The command" until the end of the line.  If you just want to skip certain lines, you would be better off doing "next if ($line =~ m{pattern})".

What are you trying to do with line 14?  It will just replace spaces with newlines - it will not split those entries into the accounts array (which may or may not be what you want to do) - it will just make the "line" have embedded newlines in it.
0
 
LVL 3

Author Closing Comment

by:_-W-_
ID: 36714737
Perfect! You where right! The key changes with foreach loop now & it works perfectly!
0
 
LVL 84

Expert Comment

by:ozo
ID: 36715538
How are you changing the key in the loop?
0
 
LVL 84

Expert Comment

by:ozo
ID: 36715648
If you are changing the key sequentially, it may suggest that you wanted an array rather than a hash.

If you wanted multiple values in the same key, you may have wanted something more like
push @$hash{"key"},$line;
or
$hash{"key"}.=$line;
Although those could have been done more easily with
$hash{key}=[@accounts];
and
$hash{key}=join'',@accounts;
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question