Solved

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

Posted on 2011-09-27
5
323 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-_
5 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 500 total points
Comment Utility
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
Comment Utility
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-_
Comment Utility
Perfect! You where right! The key changes with foreach loop now & it works perfectly!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
How are you changing the key in the loop?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Companies that have implemented Microsoft’s Active Directory need to ensure that the Active Directory is configured and operating properly. If there are issues found and not resolved, it eventually leads the components to fail or stop working and fi…
Mapping Drives using Group policy preferences Are you still using old scripts to map your network drives if so this article will show you how to get away for old scripts and move toward Group Policy Preference for mapping them. First things f…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now