in reply to File reading with hashes
I do not get an error from your code. I have made a few modifications to improve layout and readability which you may find interesting. I have not really changed the functionality other than adding a die on being unable to open FH2 as suggested by mtmcc.
use strict; use warnings; my %users = %{user_paths()}; foreach my $user (keys %users) { if (-e "$users{$user}/MY_FILE") { open (FH2, "<$users{$user}/MY_FILE") or die "could not open $u +sers{$user}/MY_FILE $!"; while (my $line = <FH2>) { if ($line =~ /BASE_PORT=(\d+)/) { print "$user --> $1"; } } } } sub user_paths { my %users; open (FH1, "</etc/passwd") || die "Can't Open : $!"; while (<FH1>) { my @user = split (/:/); if ( $user[2] > 500 ) { $users{$user[0]} = "$user[5]"; } } return \%users; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: File reading with hashes
by Bindo (Acolyte) on Aug 08, 2013 at 08:30 UTC | |
by Random_Walk (Prior) on Aug 08, 2013 at 09:32 UTC | |
by Bindo (Acolyte) on Aug 08, 2013 at 10:01 UTC | |
by rnewsham (Curate) on Aug 09, 2013 at 07:31 UTC | |
by Random_Walk (Prior) on Aug 08, 2013 at 11:22 UTC | |
by Bindo (Acolyte) on Aug 09, 2013 at 12:04 UTC | |
by poj (Abbot) on Aug 09, 2013 at 13:21 UTC |