Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains the steps required to use the default Photos screensaver to display branding/corporate images
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…
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 tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

733 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