rand() with less decimals.

Discussion in 'Perl Misc' started by Olaf, Sep 8, 2006.

  1. Olaf

    Olaf Guest

    Hello...

    $a=rand(10);
    I need that $a have just two decimals and not 14!

    Thank you.

    (My perl is correctly compiled)
    Olaf, Sep 8, 2006
    #1
    1. Advertising

  2. Olaf

    MarkRat Guest

    Olaf wrote:
    > Hello...
    >
    > $a=rand(10);
    > I need that $a have just two decimals and not 14!


    perldoc -f sprintf

    my $a = sprintf "%.2f", rand(10);
    MarkRat, Sep 8, 2006
    #2
    1. Advertising

  3. Olaf wrote:
    > Hello...
    >
    > $a=rand(10);


    Don't use $a, it is reserved for the sort function.

    > I need that $a have just two decimals and not 14!


    ( my $x = rand 10 ) =~ s/(?<=\.\d\d)\d+//;

    my $x = int( rand( 10 ) * 100 ) / 100;




    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Sep 8, 2006
    #3
  4. Olaf

    Paul Lalli Guest

    $a and $b "reserved" for sort()

    John W. Krahn wrote:
    > Olaf wrote:


    > > $a=rand(10);

    >
    > Don't use $a, it is reserved for the sort function.


    I confess that I've never understood this warning. Can someone please
    explain to me the danger of using $a or $b outside of a sort
    subroutine? Is it just a visual indicator to the programmer that we're
    worried about, or is there an actual danger of data being affected?

    $ perl -le'
    $a = "Hello";
    my $b = "World";
    my @vals = sort { length $a <=> length $b} qw(a bc alpha beta gamma);
    print "@vals";
    print "$a $b";
    '
    a bc beta alpha gamma
    Hello World

    Doesn't look to me like $a is being affected by sort. Presumably,
    sort() localizes the global value, and never touches the lexical at
    all. So what's the issue?

    Thanks,
    Paul Lalli
    Paul Lalli, Sep 8, 2006
    #4
  5. Olaf

    Paul Lalli Guest

    Re: $a and $b "reserved" for sort()

    wrote:
    > Paul Lalli wrote:
    >
    > > $ perl -le'
    > > $a = "Hello";
    > > my $b = "World";
    > > my @vals = sort { length $a <=> length $b} qw(a bc alpha beta gamma);
    > > print "@vals";
    > > print "$a $b";
    > > '
    > > a bc beta alpha gamma
    > > Hello World
    > >
    > > Doesn't look to me like $a is being affected by sort. Presumably,
    > > sort() localizes the global value, and never touches the lexical at
    > > all. So what's the issue?

    >
    >
    > Your example produces no warnings or errors
    > but with this:
    >
    > #!perl
    > use strict;
    > use warnings;
    >
    > our $a;
    > my $b;
    >
    > $a = 'foo';
    > $b = 'bar';
    >
    > my @sorted = sort { $a <=> $b } (1, 5, 7, 6, 2, 3);
    >
    > print $a, $b, "\n";
    >
    > __END__
    >
    > I see this error:
    >
    > Can't use "my $b" in sort comparison at sor.pl line 11.


    Well how about that. And here I thought I was being clever by using a
    sort subroutine that couldn't just optimize away the $b, to make sure
    it was "seen". Mental note: be less clever.

    > And with:
    >
    > use strict;
    > use warnings;
    >
    > our $a;
    > my $b;
    >
    > $a = 'foo';
    > $b = 'bar';
    >
    > my @sorted = sort { int($a) <=> int($b) } (1, 5, 7, 6, 2, 3);
    >
    > print $a, $b, "\n";
    >
    > __END__
    >
    > I see a warning (but the proper output):
    >
    > Argument "bar" isn't numeric in int at sor.pl line 11.
    > foobar


    Interesting. Okay, thank you very much for these examples. I have no
    idea why mine generates no errors or warnings but these do, but at
    least you've reaffirmed my belief in the "don't use $a or $b" mantra.

    Thanks,
    Paul Lalli
    Paul Lalli, Sep 8, 2006
    #5
  6. Olaf

    Uri Guttman Guest

    Re: $a and $b "reserved" for sort()

    >>>>> "askc" == attn steven kuo@gmail com <> writes:

    askc> Paul Lalli wrote:
    >> I confess that I've never understood this warning. Can someone please
    >> explain to me the danger of using $a or $b outside of a sort
    >> subroutine? Is it just a visual indicator to the programmer that we're
    >> worried about, or is there an actual danger of data being affected?


    askc> our $a;
    askc> my $b;
    askc> $a = 'foo';
    askc> $b = 'bar';
    askc> my @sorted = sort { $a <=> $b } (1, 5, 7, 6, 2, 3);

    askc> Can't use "my $b" in sort comparison at sor.pl line 11.


    askc> our $a;
    askc> my $b;
    askc> $a = 'foo';
    askc> $b = 'bar';
    askc> my @sorted = sort { int($a) <=> int($b) } (1, 5, 7, 6, 2, 3);
    askc> print $a, $b, "\n";

    askc> I see a warning (but the proper output):
    askc> Argument "bar" isn't numeric in int at sor.pl line 11.
    askc> foobar

    and the other way is also an issue. $a and $b are immune to strict
    warnings since they are global and used by sort(). requiring them to be
    declared in sort callback subs and such would be a pain.

    perl -Mstrict -e '$a = $b'
    perl -Mstrict -e '$a = $c'
    Global symbol "$c" requires explicit package name at -e line 1.

    so for both the global nature of $a and $b and for being immune to
    strict, it is not a good idea to use those vars outside sort code.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Sep 8, 2006
    #6
  7. Re: $a and $b "reserved" for sort()

    On 2006-09-08 18:03, Paul Lalli <> wrote:
    > I confess that I've never understood this warning. Can someone please
    > explain to me the danger of using $a or $b outside of a sort
    > subroutine? Is it just a visual indicator to the programmer that we're
    > worried about, or is there an actual danger of data being affected?
    >
    > $ perl -le'
    > $a = "Hello";
    > my $b = "World";

    ^^^^^
    > my @vals = sort { length $a <=> length $b} qw(a bc alpha beta gamma);

    ^^
    > print "@vals";
    > print "$a $b";
    > '
    > a bc beta alpha gamma
    > Hello World


    Hmm, for me (v5.8.4 built for i386-linux-thread-multi and v5.8.8 built
    for i686-linux) this prints

    bc alpha beta gamma
    Hello World

    which is not the correct sort order.

    > Doesn't look to me like $a is being affected by sort. Presumably,
    > sort() localizes the global value, and never touches the lexical at
    > all.


    Yes, but the comparison routine will access the lexical variable $b and not
    the global which is localized by sort. Change the line "my @vals ..." to

    my @vals = sort {
    print "comparing $a and $b\n";
    length $a <=> length $b
    } qw(a bc alpha beta gamma);

    and run the code again: It will print:

    comparing a and World
    comparing alpha and World
    comparing a and World
    comparing alpha and World
    comparing a and World
    comparing gamma and World
    comparing gamma and World
    comparing gamma and World
    a bc alpha beta gamma
    Hello World

    hp


    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
    Peter J. Holzer, Sep 8, 2006
    #7
  8. Olaf <> wrote:

    > I need that $a have just two decimals and not 14!



    perldoc -q round

    Does Perl have a round() function? What about ceil() and floor()?
    Trig functions?


    > (My perl is correctly compiled)



    Ummm, OK.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 8, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri

    Formatting Decimals

    Kerri, Aug 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    406
    Steve C. Orr, MCSD
    Aug 21, 2003
  2. jiajia wu
    Replies:
    0
    Views:
    340
    jiajia wu
    Oct 1, 2009
  3. 7stud --

    rand() v. rand(0.1) ?

    7stud --, Sep 15, 2007, in forum: Ruby
    Replies:
    6
    Views:
    213
    Morton Goldberg
    Sep 16, 2007
  4. 6668
    Replies:
    0
    Views:
    135
  5. lllll
    Replies:
    0
    Views:
    116
    lllll
    Jun 8, 2009
Loading...

Share This Page