Solved

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

Posted on 2011-09-27
5
329 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

785 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