Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
my %user_dict; #stores {user : info} of all system users my @user_info; #temp list of data from USER_LIST my @online_list; #list of users currently logged in my $user; my $online; my $user_online; open (USER_LIST, 'users') or open(USER_LIST, '/etc/passwd') or die "Couldn't open: $!"; while (<USER_LIST>) { @user_info = split /:/, $_; $user_dict{$user_info[0]} = \@user_info[4,5]; } @online_list = split(' ', `/usr/bin/users`); foreach $user (keys %user_dict) { $online = 0; foreach $user_online (@online_list) { if ($user_online eq $user) { $online = 1; last; } } if ($online) { print "Online text box\n"; } else { print "Offline text box\n"; } print "User Info on $user: $user_dict{$user}\n"; } --
Instead of printing the 5th and 6th fields of /etc/passwd (homedir + shell), it prints "User Info on root: SCALAR(0x8056d08)". Could someone: 1) Tell me what I'm doing wrong 2) PLEASE comment on *how* I've coded this.I want to learn to code Perl write, and shed all the Python-esque techniques I've learned. If you were going to write the above, how would you do it? Should I be declaring all my variables at the top, or should I declare them throughout the program? TIA
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: newbie - hash - array?
by rgmoore (Initiate) on Feb 11, 2000 at 11:56 UTC | |
|
Re: newbie - hash - array?
by setantae (Scribe) on Feb 11, 2000 at 08:22 UTC | |
by chromatic (Archbishop) on Feb 11, 2000 at 09:31 UTC | |
|
Re: newbie - hash - array?
by Anonymous Monk on Feb 11, 2000 at 11:17 UTC |