SPUG: Another program head scratcher

Parr, Ryan Ryan.Parr at wwireless.com
Sat Jul 13 15:46:54 CDT 2002



foreach $line(@ipx) { 
^^ You probably should take Aaron Salo's advice and use your MySQL
capabilities, why read the whole file in and loop through each line? The
database is going to do this much more efficiently than you will.

  @ipstuff = split(/\\n/, $line);	
^^ Why are you escaping the newline? \\n will match a literal '\n', and why
would you split on a newline? Since the default record seperator *is* a
newline, there aren't any real newlines in your lines. Just chomp($line).
Otherwise your @ipstuff array is just one value, and a split into an array
then looping over that array is a lot more work than you need to do.

  foreach $ipl (@ipstuff) {
^^ See commment directly above

    ($cnt, $ipadd) = split(/\|/, $ipl);
^^ See first comment

You can't do this:

$ip1 = '12.12.12.12';
$ip2 = '12.12.12.13';

if($ip1 == $ip2)...

Both ip's are strings, you can't do the numeric comparisons. Which is what
your error messages are still stating... You can do this though:

$num1 = "12\n";
$num2 = "12\n";
if($num1 == $num2)... <-- this will work

The newlines aren't your issue.

In short, you probably aren't going to find a better way to do this than
with a database. It's just easier and more efficient, and your flatfile
could become huge and weigh down your process when you read the entire thing
in on every single request to this page.

However, this is probably the same issue you posted about a couple days ago.
Most people aren't on static IP's anyways... you'll have better luck with
cookies. Than you can use a little bit of javascript and make the server
side process moot. If you use IP's for determining whether or not a person
has entered, than you could end up allowing the same person to enter
multiple times and blocking out people who've never entered at all.

-- Ryan Parr

"I hate quotations." -- Ralph Waldo Emerson


-----Original Message-----
From: Sweethomes [mailto:sweetsue at sweethomes.com] 
Sent: Saturday, July 13, 2002 12:28 PM
To: Seattle Perl User's Group
Subject: RE: SPUG: Another program head scratcher


I think I pinpointed one problem anyway.  The coding is:

foreach $line(@ipx) {
  @ipstuff = split(/\\n/, $line);
  foreach $ipl (@ipstuff) {
    ($cnt, $ipadd) = split(/\|/, $ipl);

The errors I get are:

Argument "\n" isn't numeric in numeric ne (!=) at test2.cgi line 15.
Argument "12.12.12.12" isn't numeric in numeric ne (!=) at test2.cgi line
15.

Do I need to strip out the returns somehow?  And it just seems like it's not
splitting out the lines as it should.

Susanne

-----Original Message-----
From: owner-spug-list at pm.org [mailto:owner-spug-list at pm.org]On Behalf Of
Creede Lambard
Sent: Saturday, July 13, 2002 10:52 AM
To: Sweethomes
Cc: Seattle Perl User's Group
Subject: Re: SPUG: Another program head scratcher


On Sat, 2002-07-13 at 10:25, Sweethomes wrote:
> If you guys are sick of me, let me know LOL (I've been learning a lot 
> the past week or so, PHP and MySQL are more my forte, thank you for 
> your help lately!).  I have a program (see near bottom) that is 
> supposed to open a file, read it, check to see if an ip is already 
> entered or if the count
has
> reached 2 and if so, print out a quick message saying they can't enter 
> as they already have.  Then, if it's a new ip and the count is less 
> than 2,
it
> will record the ip address, increment the count then (and this isn't 
> in there yet) proceed to the "contest" page.  It's only doing the 
> count to 1 over and over and it's not picking up that an ip has been 
> there before. Here are the errors I see when trying to process it:
>
> Use of uninitialized value in numeric eq (==) at test2.cgi line 15. 
> Argument "67.40.29.147" isn't numeric in numeric eq (==) at test2.cgi 
> line 15. Argument "0|\n" isn't numeric in numeric eq (==) at test2.cgi 
> line 15. Use of uninitialized value in string ne at test2.cgi line 75, 
> <RDIR> line
1.
>

OK, I'll admit I didn't get any farther than this, because the "isn't
numeric" is a CLUE in capital letters. :)

When you're dealing with numbers, you use "==". When you're dealing with
strings, you use "eq". Unless, of course, you decide to use =~ instead. If I
remember right =~ is more expensive than eq, but also more flexible because
of the regular expressions (as long as you don't let the "explosion in a
punctuation factory" stuff scare you, of course). Just to use a trivial
example, you can replace

if (uc(substr($answer, 0, 1)) eq 'Y') { return 1; }

with

if ($answer =~ /^y/i) { return 1; }


--
 * .~. `(
----------------------------------------------------------------
` / V \  . Creede Lambard                : Just who is this General
Failure
 /(   )\   creede at penguinsinthenight.com :   and why is he reading my
disk?
  ^^-^^
----------------------------------------------------------------

Perl Programmer and Linux Sysadmin, reasonable rates. Inquire within. GPG
key at http://www.penguinsinthenight.com/creede_public_key.asc



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list