p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Perl (http://p2p.wrox.com/forumdisplay.php?f=196)
-   -   scalar and reverse (http://p2p.wrox.com/showthread.php?t=51827)

crmpicco December 21st, 2006 08:38 AM

scalar and reverse
Can anyone assist in explaining what the function 'commafy' is doing?
there are a few things in there i dont understand, i.e. the scalar and reverse pieces of code.
my usage of the function:


comm(sprintf("%.2f", $byviewprice{$view}))
a typical example of the value of '$byviewprice{$view}' is 501.


sub comm {

    my $text = shift;

    $text = reverse $text;
    $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;

    if ($text eq "") {
        $text = 0;

    return scalar reverse $text;


ciderpunx December 30th, 2006 09:52 PM


scalar reverse reverses the order of the chars in your string i.e. if your string was 'string' it'd end up being 'gnirts'. In a list context it'd return the items in the list in reverse order. And with a hash it inverts the hash. cf:



Don't Stand on your head - you'll get footprints in your hair

crmpicco February 16th, 2007 08:18 AM

great, cheers again.
also found:
Perl's reverse() function is used to reverse the order of an array. It should be noted that the function returns a reversed array but does not actually reverse the array passed into the function.


All times are GMT -4. The time now is 02:47 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.