in reply to reading from file.

G'day scripter87,

You haven't really provided sufficient information. "it works" and "it dont" and spectacularly useless error reports. You haven't shown how you're testing this: i.e. what you're using for input. You haven't shown your data: there could be line ending and embedded whitespace issues. A better question gets better answers: take a look at the guidelines in "How do I post a question effectively?" to see how you can achieve this.

There's issues with the code as posted. It will print "not valid" for every record in names.txt until it either finds a match or reaches EOF. I see Eily pointed out an issue with $names: you said this was a typo; if you copy and paste your actual code, this won't happen. Do you really want to open/read/close the entire file for every "my $input = <STDIN>;"? Do you allow more than one attempt?

Here's a potentially better way to do what you want:

#!/usr/bin/env perl use strict; use warnings; use autodie; my $name_file = 'names.txt'; open my $name_file_fh, '<', $name_file; chomp(my @names = <$name_file_fh>); close $name_file_fh; print 'Enter login name: '; chomp(my $input = <STDIN>); print "Invalid!\n" unless grep { $_ eq $input } @names;

Note that you can wrap the 'Enter login name: ' part in a loop to allow multiple attempts without having to reread the file. Not shown in your posted code, so I haven't tried to make guesses here, but you'll need to code some sort of successful login actions.

Update: Prompted by Eily's mention of a hash and exists, which I saw after posting, this would be an improvement on what I originally provided:

#!/usr/bin/env perl use strict; use warnings; use autodie; my $name_file = 'names.txt'; open my $name_file_fh, '<', $name_file; my %name = map { chomp; $_ => 1 } <$name_file_fh>; close $name_file_fh; print 'Enter login name: '; chomp(my $input = <STDIN>); print "Invalid!\n" unless exists $name{$input};

-- Ken

Replies are listed 'Best First'.
Re^2: reading from file.
by Random_Walk (Prior) on Nov 14, 2013 at 01:37 UTC

    Unless the script is long running the hash solution is still sub optimal. Read the name, then look through the file, will on average read half the file for a valid name. The hash solution must always read the entire file. A better way to store the file could improve things even more, but perhaps we get silly...

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      I noted there was insufficient information. I asked whether multiple attempts were allowed. I presented a clearly stated potential solution.

      Allowing multiple attempts is the norm. I raised the absence of code to achieve this (i.e. while loop) along with a lack of any handling of a successful login attempt.

      Both the array and hash solutions read the entire file; obviously, this is not something specific to the hash solution. The point I made was that this was only done once regardless of the number of attempts.

      -- Ken

        The code that I put up here isnt the exact code I am using. I generally just wanted the idea of what to do. Thats why it was typed. See I have a text file with the below data and must be done in that format, was reading into hashes seems the way to go, but will just arrays work to. The exact problem I have is. I am trying to ask a user for the username and password and retrieve them from the file and continue with the program if they validate.
        name1 pass1 name2 pass2