Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^4: File reading with hashes

by Bindo (Acolyte)
on Aug 09, 2013 at 12:04 UTC ( #1048757=note: print w/replies, xml ) Need Help??

in reply to Re^3: File reading with hashes
in thread File reading with hashes

Thank you very much R, It worked. :) I owe a big apology to people who tried to help me out because I found out that the string I was looking for in the "MY_FILE" file was "SREG_PORT" not "BASE_PORT". I have wasted your time and mine over some silly thing that I should have easily realized. Sorry gentlemen

Mr R, with right key word being searched the script I initially mentioned in my question also works but I do get that readline error message printed only once on the screen. I tried tweaking the scrit a bit but no luck. Im gonna use your script as it is very clean and solid, but Id still like to realize what I have done wrong to get that error.

following is the output when I run my version of script

supun --> 40000 sam --> 25000 readline() on closed filehandle $fh at ./ line 11. binasha --> 10000 bundy --> 37000 binjiya --> 5000

Following is my version of script

my $fname = 'MY_FILE'; my %users = %{user_paths()}; foreach my $user (keys %users) { my $fh; if (-e "$users{$user}/$fname") { open ($fh, '<', "$users{$user}/$fname"); while (my $line = <$fh>) { if ($line =~ /S_REG_PORT_NO=(\d+)/) { print " $user --> $1\n"; } } } } sub user_paths { my %users; open my $fh, '<', "/etc/passwd" || die "Can't Open : $!"; while (<$fh>) { my @user = split (/:/); if ( $user[2] > 500 ) { $users{$user[0]} = "$user[5]"; } } return \%users; }

Only different thing Im doing here is that Im checking the existence of the file first with the "-e". Please help me figure this out. I just like to learn is all :) thanks again for helping out gentlemen.

Replies are listed 'Best First'.
Re^5: File reading with hashes
by poj (Monsignor) on Aug 09, 2013 at 13:21 UTC
    Is it possible a user has a folder called MY_FILE ? . If so, this code should find it .
    foreach my $user (keys %users) { my $fh; my $file = "$users{$user}/$fname"; if (-d $file){ print "$file is directory for user $user\n"; } elsif (-e $file ) { open ($fh, '<', $file) or warn "Could not open $file for user $use +r :$!" ; while (my $line = <$fh>) { if ($line =~ /S_REG_PORT_NO=(\d+)/) { print " $user --> $1\n"; } } } else { print "$file does not exist for user $user\n"; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048757]
[Discipulus]: good morning monks! today is haoliday in Eataly
[Eily]: good morning Discipulus
[hippo]: Lucky Eatalians!
[Eily]: was that a typo or is there a special meaning behind "haoliday"?
marto waves
[Discipulus]: was a type of typo.. April 25 liberation day. Liberation from nazi fascist (liberation from ourselves..)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2018-04-25 08:03 GMT
Find Nodes?
    Voting Booth?