Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to A few simplifications [Re: a problem with a point...] by roboticus
in thread a problem with a point... by Bass-Fighter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found