in reply to Duplicate data record

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"

Replies are listed 'Best First'.
Re: Re: Duplicate data record
by Juerd (Abbot) on Dec 20, 2001 at 18:45 UTC
    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"

        I'm Juerd, not vroom :)

        You used a die() in your unless-block anyway, so your code will also cause a 500 error. I think having structured code when a simple "or die" suffices is overkill...
        I agree on your script being more clear to those who already know another language, but did you learn Perl to code the way you always did? I sure didn't: I learnt perl because I like the perlish way. But that's personal, and I can think of good reasons to code in an unperlish way.

        P.S. There's nothing greater than the invisible $_ game - if you know the rules.

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

        Here is my phone.cgi I have a phone.html to call this cgi when they summit it will write to file Data/phone.txt But i really do not want them to add more than one time like:
        mike:miketyson@yahoo.com:123-2323-324 bill:billyong@hotmail.com:434-3213-321 mike:miketyson@yahoo.com:123-2323-324 briney:britney@yahoo.com:343-2322-111 mike:miketyson@yahoo.com:123-2323-324
        It should tell mike is already enter in our database before they write to Data/phone.txt they check to see any mike with the same email and phone or not if the same they give alert. If not then add. Thanks