Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: reading from file.

by ramlight (Friar)
on Nov 13, 2013 at 21:00 UTC ( #1062473=note: print w/replies, xml ) Need Help??


in reply to reading from file.

It looks to me like you may have your logic backwards. For any list of with more than one name, it will always print "not valid". I suspect that you meant something like
if input_name is in list: print "valid" else: print "not valid" This could be implemented with the following code: if (grep(/^$input$/, @names) { print "Valid\n"; } else { print "Not valid\n"; }
There are a number of other areas that could be cleaned up. For example, you probably should read the list of valid names before getting your name to test. A little more consistency in your use of brackets would also make your code easier to figure out (both for you and for us.)

Replies are listed 'Best First'.
Re^2: reading from file.
by scripter87 (Novice) on Nov 13, 2013 at 21:10 UTC
    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.

      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!

      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 :)

      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://1062473]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-08-14 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?