Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: reading from file.

by scripter87 (Novice)
on Nov 13, 2013 at 21:10 UTC ( [id://1062475]=note: print w/replies, xml ) Need Help??


in reply to Re: reading from file.
in thread reading from file.

thanks for the feedback. The basic thing I am trying to grasp is.. how to read files from a file for login purposes, so if I enter a name that is not in the file it obviously shouldnt work but as I said at the moment if I have just one line in the file it works ok but if more than one it doesnt.

Replies are listed 'Best First'.
Re^3: reading from file.
by Kenosis (Priest) on Nov 13, 2013 at 21:32 UTC

    Although there are several solutions for your task, consider the following:

    use warnings; use strict; my $names = 'names.txt'; my ( $found, $name ); print 'Enter a name: '; chomp( my $input = <STDIN> ); open my $fh, '<', $names or die "Cannot open $names: $!\n"; while ( $name = <$fh> ) { chomp $name; if ( $input eq $name ) { $found++; last; } } close $fh; if ($found) { print qq{The name "$input" was found.\n}; } else { print qq{The name "$input" was not found.\n}; }

    Note that:

    • The name to search for is requested first.
    • chomp is used to remove the trailing input record separator (usually \n), from both the entered name and the names from the file.
    • The names' file is read once; no hash or array is necessary to 'hold' the names (Why process the list twice?). A flag's set and the while loop is immediately exited if the name is found.
    • Appropriate responses are given depending upon the value of $found.

    Hope this helps!

Re^3: reading from file.
by Eily (Monsignor) on Nov 13, 2013 at 21:36 UTC

    You should have tried the solution ramlight gave you, it does look like what you were looking for. Maybe grep { $_ eq $input } @names instead of grep(/^$input$/, @names) would avoid some trouble with the metacharacters in $input though. And if you don't understang what it does, try and read grep's documentation :).

    Another way to do it would be using a hash, because you have exists to check if a name exists (obviously) in it.

    my @nameList = qw/Anna Beatrix Claude Damian/; my %names = map { $_ => 1 }, @nameList; # associate the value 1 to eac +h name in @namelist. Actually the value could be anything in this cas +e print "Anna exists" if exists $names{"Anna"}; print "Paul doesn't exist" unless exists $names{"Paul"};

    And as taint already stated, chomp your strings :)

Re^3: reading from file.
by taint (Chaplain) on Nov 13, 2013 at 21:18 UTC
    Thinking out loud;

    chomp ...
    foreach line as line ...

    Well, something like that.

    --Chris

    #!/usr/bin/perl -Tw
    use Perl::Always or die;
    my $perl_version = (5.12.5);
    print $perl_version;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1062475]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-26 03:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found