Re: HoH problem representing a data structure.

[email protected] ("Rodrick Brown")
Newsgroups perl.beginners
Message-ID <[email protected]>
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.



>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.