[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2011-09-27
5
Medium Priority
?
342 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 2000 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 27

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
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 video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

650 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