Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

a problem with a point...

by Bass-Fighter (Beadle)
on Jan 20, 2009 at 10:32 UTC ( [id://737533]=perlquestion: print w/replies, xml ) Need Help??

Bass-Fighter has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,

I posted this morning something to get an email adress... now I am a bit further, but I walk against a problem.
this is my code at this moment:

#!usr/bin/perl use strict; use warnings; use DBI; my $adres; $adres = '<erik.pietersen@somefactory.com>'; print $adres; my $db = DBI->connect("DBI:Pg:dbname=bigone; port=5432", 'postgres', ' +',{ RaiseError => 1, AutoCommit => 0 }) or die "kon de database niet openen!"; my ($rowref, $ff, @bigmail); my $st=$db->prepare("SELECT email FROM bigone WHERE email!=''"); $st->execute(); while($rowref= $st->fetchrow_hashref){ $bigmail[$ff]= $rowref->{'email'}; $ff++; } my $zz; my $fg = @bigmail; my $pl=0; my $ecode; for (my $i=0; $i<$fg; $i++){ $ecode= $bigmail[$pl]; $pl++; if ($adres=~ /<$ecode>/){ $zz=$ecode; } } print $zz; my $relcode; $st=$db->prepare("SELECT relcode FROM bigone WHERE email = $zz "); $st->execute(); while($rowref= $st->fetchrow_hashref){ $relcode = $rowref->{'email'}; } print $relcode; $db->disconnect;
this is a my tryprogram, that is why I defined an email myself, but now I get this error:

Use of uninitialized value in array element at equal.txt line 16.
DBD::Pg::st execute failed: ERROR: Relation "erik" does not exist
DBD::Pg::st execute failed: ERROR: Relation "erik" does not exist
<erik.pietersen@somefactory.com>erik.pietersen@somefactory.com
I think it has something to do with the "." after erik.
I have 3 points where I print what I must get.
the first two gives the output I want, so the problem lies at the last select with the database.

who knows how to let it work?

Replies are listed 'Best First'.
Re: a problem with a point...
by moritz (Cardinal) on Jan 20, 2009 at 10:39 UTC
    Use of uninitialized value in array element at equal.txt line 16.

    You can get rid of this warning by initializing $ff to zero.

    $st=$db->prepare("SELECT relcode FROM bigone WHERE email = $zz "); $st->execute();

    Don't ever interpolate data into your SQL strings, you'll only have problems with quoting. Use placeholders instead:

    $st=$db->prepare("SELECT relcode FROM bigone WHERE email = ?"); $st->execute($zz);

    Much safer!

      ohw, yes I see:D
      I was forgotten that declare $ff to zero...
      but that other thing I didn't know, another thing learnt today

      Thankx:D

A few simplifications [Re: a problem with a point...]
by roboticus (Chancellor) on Jan 20, 2009 at 14:01 UTC
    Bass-Fighter:

    I'm in a verbose mood today, so please forgive the length of this message. While reading through your code, I thought I'd offer a few random suggestions:

    First, I notice that you're using the construct:

    my ($rowref, $ff, @bigmail); ... snip ... while($rowref= $st->fetchrow_hashref){ $bigmail[$ff]= $rowref->{'email'}; $ff++; }

    You're using the variable $ff only to help you load the @bigmail array, so you're trying to keep $ff synchronized with the index of the next slot in @bigmail to fill. Perl gives you the equivalent $ff for free: Using @bigarray in scalar context evaluates to the number of items in the array--and since perl numbers array slots from 0 .. N-1, the number of items in the array is the index of the next available slot: N. So you can eliminate $ff like so:

    my ($rowref, @bigmail); ... snip ... while($rowref= $st->fetchrow_hashref){ $bigmail[@bigmail]= $rowref->{'email'}; }

    And since you're just adding an item to an array, you could use push or shift to add the item to the array. (One adds the item to the end of the array, and one inserts it at the beginning of the array. If you don't care about order, it doesn't matter which one you use.) So the statement:

    $bigmail[@bigmail]= $rowref->{'email'};

    could also be expressed as:

    push @bigmail, $rowref->{'email'};

    Next, regular expressions incur some overhead. Using them to match a single constant value makes perl compile a regular expression and then execute it for each string. If you change this:

    if ($adres=~ /<$ecode>/){

    to this:

    if ($adres eq "<$ecode>"){

    you can avoid that little bit of overhead. I wouldn't go to the trouble of changing it, but I thought I should mention it to you.

    There are a couple of things about the following snippet I should mention:

    my $zz; my $fg = @bigmail; my $pl=0; my $ecode; for (my $i=0; $i<$fg; $i++){ $ecode= $bigmail[$pl]; $pl++; if ($adres eq "<$ecode>"){ $zz=$ecode; } }

    First, you're introducing a new variable ($fg) to hold the number of items in your array. But you could just use @bigmail instead, and your code would be a bit easier to read because I wouldn't have to figure out what the variable $fg contains. Making that change gives you:

    my $zz; my $pl=0; my $ecode; for (my $i=0; $i<@bigmail; $i++){ $ecode= $bigmail[$pl]; $pl++; if ($adres eq "<$ecode>"){ $zz=$ecode; } }

    Next, the variable $pl isn't necessary, as your loop variable $i is serving the same purpose: stepping through the available item indexes in @bigmail. So you can remove it, giving you:

    my $zz; my $ecode; for (my $i=0; $i<@bigmail; $i++){ $ecode= $bigmail[$i]; if ($adres eq "<$ecode>"){ $zz=$ecode; } }

    But don't do it this way, either. Perl gives you a nicer version of the for statement--it lets you use the items in the array directly, rather than having to use an artificial variable. So you can write that loop using the nicer for statement like this:

    my $zz; for my $ecode (@bigmail){ if ($adres eq "<$ecode>"){ $zz=$ecode; } }

    I could go on with grep to simplify that loop further, but I'll leave that as an exercise to the reader. Instead, I'll just mention that the variable name $zz isn't very informative. You can make your code easier to understand by using more descriptive variable names.

    Ah, well ... off to work! I hope you find something in here you can use.

    ...roboticus

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://737533]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found