Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding exact name from list 3

Status
Not open for further replies.

ETbyrne

Programmer
Dec 27, 2006
59
US
Hi, I'm having trouble with a script that looks thru authorlist.txt and looks to see if $author equals any of the lines.

But, so far no matter what you type in for $author, it says that $author allready exists!

Here's the sorting part of the script:

Code:
my $author = param('author') or die "You have to enter a Nick Name.";

open(FILE, "<authorlist.txt") or die "Can't open authorlist.txt: $!";
seek(FILE,0,0);
@file = <FILE>;
chomp($key = $author);
foreach $el (@file)
  {
  if ($key == $_)
    {
      print "That username allready exist, click back and try a different one\n";
      exit;
    }
  }

And here is authorlist.txt

Code:
Evan
Max T1
Pow~V1
HomeFry
Moooo

As you can see, there is not even the letter J in authorlist.txt, but if you enter J, it will say that author name allready exists!

Also, I would like it if it would match exact case, so if you enter the name "Moo" it does not say that it allready exists because "Moooo" allready does.

_______________________________________

You don't know me.
 
try:

if ($key eq $_)

'eq' is for strings
'==' is for numbers


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Now it is not finding matches at all, no matter what you type in.

_______________________________________

You don't know me.
 
you probably need to chomp $el before comparing it to $key.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Also, you said exact, which it will have to be, including upper and lower case.

'Evan' does not equal 'evan'

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It's not going to matter what operator you're using - where is $_ getting set? The foreach loop is aliasing the elements in @file to $el.

Code:
[blue][b]foreach $el (@file)[/b][/blue]
  {
  if ([red][b]$key == $_[/b][/red])
    {
      print "That username allready exist, click back and try a different one\n";
      exit; [green]# This should probably be [b]last[/b][/green]
    }
  }
 
It's not going to matter what operator you're using - where is $_ getting set? The foreach loop is aliasing the elements in @file to $el.

hehehe... yea, that too.

Kevin --> [flush]


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok, my script is doing something very odd. If I use the == sign it allways says it finds a match. If I use eq it allways doesn't find a match.

Here's what I got.

Code:
my $author = param('author') or die "You have to enter a Nick Name.";

open(FILE, "<authorlist.txt") or die "Can't open authorlist.txt: $!";
seek(FILE,0,0);
@file = <FILE>;
chomp($key = $author);
chomp($el);
foreach $el (@file)
  {
  if ($key eq $el) #or: if ($key == $el)
    {
      print "That username allready exist, click back and try a different one\n";
      exit;
    }
  }


_______________________________________

You don't know me.
 
As Kevin says, '==' is incorrect: that's for numbers. You definitely want 'eq'.

The problem is that you're chomping $el before it's defined. You should be calling chomp inside the foreach loop.

This is one of those cases where it's an EXCELLENT idea to get used to using strict and warnings. I see you're already declaring some of your variables with "my" - you should extend that habit to all your variables and add in "use strict;" at the top of your strict. That would have alerted you to the fact that you're trying to chomp $el before it's defined.
 
Thanks everybody, it works now! ^_^

Oh, and this is the final script.

Code:
#!/usr/bin/perl
use CGI qw(:standard);
use Fcntl qw(:flock :seek);
use strict;

my $author = param('author') or die "You have to enter a Nick Name.";

open(FILE, "<authorlist.txt") or die "Can't open authorlist.txt: $!";
seek(FILE,0,0);
my @file = <FILE>;
chomp(my $key = $author);

foreach my $el (@file)
  {
  chomp($el);
  if ($key eq $el)
    {
      print "That username allready exist, click back and try a different one\n";
      exit;
    }
  }

it then goes on and does some other stuff.

_______________________________________

You don't know me.
 
Oh, and how could I modefy this so you search for a file in a directory?

This is what I got so far, but it allways doesn't find $file.txt

Code:
#!/usr/bin/perl
use strict;
use CGI qw(:standard);

my $dir = param('dir') or die "You have to enter the folders Name.";

my $file = param('file') or die "You have to enter the file you are looking for.";

my $newfile = "$file.txt";

opendir(FH, "$dir") or die "The username that you entered does not exist, check your spelling and try re-typing it.";
my @file = <FH>;
chomp(my $key = $newfile);

foreach my $el (@file)
  {
  chomp($el);
  if ($key eq $el)
    {
      print "Content-type: text/html\n\n";
      print "<html><head><title>Portal Manager</title></head>\n";
      print "<body bgcolor=\"silver\">\n";
      print "<h2>Welcome to the $dir Directory</h2>\n";
      print '</body></html>\n';
      exit;
    }
  }


print "That file does not exist, try typing in a new one.";

I allways get "That file does not exist, try typing in a new one."


_______________________________________

You don't know me.
 
I suggest that you create more useful error messages. Currently you're only outputting basic information concerning the nature of each fault. If something is wrong with the filename, be sure to include the filename in the error message. This will enable you to more accurately assess whether the error is on the user side or if it's in the logic of your program.

Code:
print "'$newfile' does not exist.  Please try again.";

Also, chomping $key does not do anything:

Code:
chomp(my $key = $newfile);

$newfile is initialized with a '.txt' extension, so you already know that it does not end in a return character. Maybe you mean to chomp "$file" instead?
 
it's a classic example of using too may variables:

Code:
my [b]$author = param('author')[/b] or die "You have to enter a Nick Name.";

open(FILE, "<authorlist.txt") or die "Can't open authorlist.txt: $!";
seek(FILE,0,0);
my @file = <FILE>;
chomp([/b]my $key = $author[/b]);
foreach my $el (@file)
  {
  chomp($el);
  if ($key eq $el)

$key is not needed, it's just a copy of $author. Use $author to compare to $el:

Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]CGI[/green] [red]qw([/red][purple]:standard[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]Fcntl[/green] [red]qw([/red][purple]:flock :seek[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$author[/blue] = [maroon]param[/maroon][red]([/red][red]'[/red][purple]author[/purple][red]'[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]You have to enter a Nick Name.[/purple][red]"[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]FILE, [red]"[/red][purple]<authorlist.txt[/purple][red]"[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open authorlist.txt: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/seek.html][black][b]seek[/b][/black][/url][red]([/red]FILE,[fuchsia]0[/fuchsia],[fuchsia]0[/fuchsia][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@file[/blue] = <FILE>[red];[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]FILE[red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$exists[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$el[/blue] [red]([/red][blue]@file[/blue][red])[/red] [red]{[/red]
  [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][blue]$el[/blue][red])[/red][red];[/red]
  [olive][b]if[/b][/olive] [red]([/red][blue]$author[/blue] eq [blue]$el[/blue][red])[/red] [red]{[/red]
      [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]That username allready exist, click back and try a different one[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
      [blue]$exists[/blue] = [fuchsia]1[/fuchsia][red];[/red]
      [olive][b]last[/b][/olive][red];[/red]
  [red]}[/red]
[red]}[/red]
[olive][b]unless[/b][/olive] [red]([/red][blue]$exists[/blue][red])[/red] [red]{[/red]
   [black][b]print[/b][/black] [red]"[/red][purple]Your username '[blue]$author[/blue]' has been approved! Hip-hip-hooray![/purple][red]"[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]CGI - Simple Common Gateway Interface Class[/li]
[li]Fcntl - load the C Fcntl.h defines[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok, so I don't nead to chomp anything for this. It still doesn't work, of course. Hmmm...

Oh, I have another (yes another) question. I've been looking and looking thru my books and they don't say anything on writing permisions to folders after they are created. How can you do this? (From a script)

_______________________________________

You don't know me.
 
Oops, you must have posted at the same time as me Kevin, I'll try your script.

_______________________________________

You don't know me.
 
if it till does not work post the data you are using, the input data and some of the file data.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks, that worked great! ^_^

_______________________________________

You don't know me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top