Re: HoH problem representing a data structure.
[email protected] (Aruna Goke)
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
Rodrick Brown wrote:
> I'm trying to fully understand references so I created a data structure to
> stores the output of users on my system who share home dirs.
> I use a HoH to represent this data set but its not working as expected.
> Conceptually I can visualize how the data should look but I cant get the
> output any assistance will be appreciated thanks.
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
> use Data::Dumper;
> my $file = '/etc/passwd';
> my $hash;
> my ($user, $homeDir);
> my $count=0;
> open(my $fh, "<", $file) or die("Fatal error unable to read $file: $!");
> while(<$fh>) {
> next if /^#/;
> ($user, $homeDir) = (split /:/,$_)[0,5];
> $hash->{$homeDir} = $user;
> }
> print Dumper($hash);
>
> My output looks like the following:
>
> $VAR1 = {
> '/var/imap' => '_cyrus',
> '/var/empty' => '_unknown',
> '/Library/WebServer' => '_www',
> '/var/xgrid/agent' => '_xgridagent',
> '/var/virusmails' => '_amavisd',
> '/var/spool/cups' => '_lp',
> '/var/pcast/agent' => '_pcastagent',
> '/var/root' => 'daemon',
> '/var/spool/postfix' => '_postfix',
> '/var/teamsserver' => '_teamsserver',
> '/var/pcast/server' => '_pcastserver',
> '/var/xgrid/controller' => '_xgridcontroller',
> '/var/spool/uucp' => '_uucp'
> };
>
>
> If I replace line 13 with the following the data structure looks some what
> like what I wanted but it still not correct.
> $hash->{$homeDir}->{$user} = 0; basically the value 0.
>
> $VAR1 = {
> '/var/imap' => {
> '_cyrus' => 0
> },
> '/var/empty' => {
> '_spotlight' => 0,
> '_appserver' => 0,
> '_ard' => 0,
> '_appowner' => 0,
> '_mcxalr' => 0,
> 'nobody' => 0,
> '_qtss' => 0,
> '_securityagent' => 0,
> ...
> ...
> ...
>
#!/usr/bin/perl
use warnings;
use strict;
my $hash;
my $files = 'hohtest.txt';
open FH, '<', $files or die "Mo ri re oo..Faili ko open: $!";
while(<FH>){
my ($user, $homedir) =(split/:/, $_)[0,5];
$hash = {$user, $homedir};
print "home directory of $user is $hash->{$user}\n";
}
hohtest.txt
rpm:x:37:37::/var/lib/rpm:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
netdump:x:34:34:Network Crash Dump user:/var/crash:/bin/bash
pcap:x:77:77::/var/arpwatch:/sbin/nologin
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
beagleindex:x:58:58:User for Beagle indexing:/var/cache/beagle:/bin/false
distcache:x:94:94:Distcache:/:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
gdm:x:42:42::/var/gdm:/sbin/nologin
quagga:x:92:92:Quagga routing suite:/var/run/quagga:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
Goksie