Rodrick Brown wrote: > On Sun, Jun 8, 2008 at 10:37 AM, Gunnar Hjalmarsson <[EMAIL PROTECTED]> > wrote: > >> Rodrick Brown wrote: >> >>> #!/usr/bin/perl -w >>> >> The -w switch is redundant, since you have "use warnings;". >> >> use strict; >>> use warnings; >>> use Data::Dumper; >>> my $file = '/etc/passwd'; >>> my $hash; >>> my ($user, $homeDir); >>> my $count=0; >>> >> Why did you declare that variable? >> >> open(my $fh, "<", $file) or die("Fatal error unable to read $file: $!"); >>> while(<$fh>) { >>> next if /^#/; >>> ($user, $homeDir) = (split /:/,$_)[0,5]; >>> $hash->{$homeDir} = $user; >>> >> You probably want: >> >> push @{ $hash->{$homeDir} }, $user; >> >> } >>> print Dumper($hash); >> > Yes please explain how exactly that line works? I know @{} dereferences an > array so it looks like your pushing each user into an anonymous array.
Exactly. The value of a hash element can only be a scalar, and so must represent a list of items using a reference to an anonymous hash. The syntax push @{ $hash->{$homeDir} }, $user; is concise because of 'autovivifaction', which will initialise the hash element with an implied $hash->{$homeDir} = []; if it doesn't already exist. You should take a look at perldoc perlref and perldoc perllol to understand further. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/