rand() with less decimals.

O

Olaf

Hello...

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

Thank you.

(My perl is correctly compiled)
 
J

John W. Krahn

Olaf said:
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
 
P

Paul Lalli

John said:
Olaf wrote:

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
 
P

Paul Lalli

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
 
U

Uri Guttman

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
 
P

Peter J. Holzer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top