SPUG: Rebuked...

Steve Laybourn otter101 at hotmail.com
Tue Jan 4 13:09:20 CST 2000


Hello again!
All right. I consider myself severely rebuked for posting such a crummy bit 
of code. But how am I gonna learn if I don't ask about it?

Ken McGlothlen was nice enough (I think) to give me the sort of 
grab-that-guy-by-the-collar-and-shake-some-sense-into-him wake-up call and 
tell me why my code was inefficient. I more or less paraphrase him now:

>So here's my rewrite of your function:
(My turn to see if I have the interpretation of this correct):
>
>	sub Cur {
>	    my( $ix ) = shift;

---Let local variable $ix equal the first element of the default array @_, 
which in this case also equals $_. Hmmm, clever, that. How would you handle 
more than one element going in to the array? @_[0], at _[1], etc.?

>	    $ix = sprintf( "%.2f", $ix );

---This bit puzzles me a little. I understand the rounding function 
represented by the "%.2f" (you described it earlier, I'm looking into that 
further), but wouldn't assigning the result to $ix write over the current 
value of $ix, thusly eliminating the whole dollar part of $ix?

>	    1 while $ix =~ s/(.*\d)(\d\d\d)/$1,$2/g;

---OK, I really had to think this one out, but here goes:
---If the regular expression evaluates to true, then do the operation. Hmmm, 
I had wanted to work with up to nine digits and two decimal places, so 
would:
---1 while $ix=~ s/(.*\d)(\d\d\d)(\d\d\d)/$1,$2,$3/g;
---be more or less correct? The "1 while ..." statement is completely new to 
me. A long time ago I would have thought it to be a label or a line number.

>	    return( "\$$ix" );

---Prefix the result with a dollar sign and return the string.

>	}
>
>Does that help?
---Indeed it does. Sounds like I could save lots of time and variable space 
doing it this way.

Again, I apologize for not being terribly good at Perl, but if I had known 
that people were going to actually answer back in this group, I would have 
joined long ago and learned to code a lot more responsibly than this!

Thanks again. I'll be sure to irritate you again very soon.

Be seeing you...
Steve Laybourn


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list