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:

Code:

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

Code:

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;
}

www.crmpicco.co.uk
www.ie7.com

ciderpunx December 30th, 2006 09:52 PM

Hallo

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:

http://perldoc.perl.org/functions/reverse.html

HTH

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com

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.

www.crmpicco.co.uk
www.ie7.com


All times are GMT -4. The time now is 11:20 AM.

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