britney has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl, just learn from a book how to use mail form and save it in a flat text file. I can make it post and save in flat text but how you write a script form like: Your name Phone Email If you submit they can check your name already in database or not. If you already in database flat text file the they say you already have in our record. This is flate text record.txt britney|123-4567|britneythuyduong@yahoo.com Please help me, Thanks,

Replies are listed 'Best First'.
Re: Duplicate data record
by strat (Canon) on Dec 20, 2001 at 18:28 UTC
    Comment to the change: I've done some errors that I correct: I'll try to use some easy code:
    my $name = "britney"; my $file = "record.txt"; unless (open (FILE, $file)){ print "Error: couldn't open file $file: $!"; } else { my $nameFound = 0; foreach my $entry(<FILE>){ if ( /^\Q$name\|/i ){ $nameFound = $entry; last; } } if ($nameFound){ my($name, $telephone, $email) = split(/\|/, $nameFound); # do something with $name, $telephone, $email if you want } else { print "not found\n"; } } # else
    I haven't tested this code, but hope that it will work...

    Best regards,
    perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"

      Hi there, I don't agree: this isn't easy code :)
      unless (open){ die } else { foo } isn't easy in my opinion. I think open or die; foo; is a lot more readable.
      split /|/ will split on either nothing or nothing, and will have the same effect as split //. I think you really meant split /\|/ there.
      And a grep might have been a lot easier:
      $found = scalar grep { /^\Q$name\|/i } <FILE>;
      But still, it's better not to read in the whole file (did you know foreach() slurps the file before processing it?)
      while (<FILE>) { $found = 1 if /^\Q$name\|/i }


      Anyway... I was just trying to point out that your code wasn't as easy as you thought it was.

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

        Hello juerd,

        as it seems to be a cgi script, imho die is more complicated (500-error) than unless with a proper structure.

        Thank you for | => \|, since it indeed is wrong.

        I know, that foreach slurps in the file at once. I wanted to avoid the game with the invisible variable $_ with while(<FILE>), and don't like while (defined ($_ = <FILE> )), because you have to know a lot about to understand it correctly.

        I decided to write the example the way I did because it might be rather clear if you already know another programming language, even if it doesn't look very 'perlish'.

        I like your code examples, and in a script maybe I'd write something like that, but for a beginner it might be more complicated because there's so much hidden behind.

        Best regards,
        perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"