Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: min $#a, $#b > $#b

by FreeBeerReekingMonk (Deacon)
on Jan 12, 2016 at 19:42 UTC ( #1152603=note: print w/replies, xml ) Need Help??


in reply to min $#a, $#b > $#b

Whoops.. Eily caught me telling lies... ok, made a minimal working program:

use List::Util qw( min ); my @A; for $_ (1..5) { push @A, $_; $max = min($#A, 0); $t = ref $#A; print "$max = min($#A,0)\n"; }

Output:
0 = min(0,0) 1 = min(1,0) 0 = min(2,0) 0 = min(3,0) 0 = min(4,0)

YOU FOUND A BUG! workaround it like this: $max = min(0+$#A, 0);

Replies are listed 'Best First'.
Re^2: min $#a, $#b > $#b
by Crackers2 (Parson) on Jan 12, 2016 at 20:47 UTC
    Adding scalar seems to fix it..
    use List::Util qw( min ); my @one = (1); for my $_in (1..5) { push @_x, "$_in"; my $x_max = min scalar($#_x), $#one; print "SET my \$x_max $x_max = min \$#_x $#_x, \$#one $#one;\n"; } SET my $x_max 0 = min $#_x 0, $#one 0; SET my $x_max 0 = min $#_x 1, $#one 0; SET my $x_max 0 = min $#_x 2, $#one 0; SET my $x_max 0 = min $#_x 3, $#one 0; SET my $x_max 0 = min $#_x 4, $#one 0;

    Though I don't quite understand why. Real bug perhaps?

      Actually anything that will initiate a new value instead of just forwarding the current one will work (like adding quotes around the variable, 0+$var, or even scalar($var), though I wasn't actually expecting this one), because the new value won't have the same magic. I did think about forcing a copy of some variables (which explains the quotes in some parts of my code), but since I had been working on the issue for a while (I started with much bigger code, I reduced it trying to find the bug), I wasn't sure at the time of my post whether I was doing some kind of silly mistake, and I never thought about forcing the copy on the parameters of min. Thanks for the answer !

      yup, real bug. happens in v5.20.2

Re^2: min $#a, $#b > $#b
by Mr. Muskrat (Canon) on Jan 12, 2016 at 21:22 UTC

    Don't ever wipe the original content of any node you've written, even if you've solved your problem; you're clarifying your issue; or you realize you just made a "D'oh!" Mark the changed/new content with the word "Update." Don't delete! Doing so is likely to make replies you've received unintelligble to future readers. Use <strike> ... </strike> if you must.
    -- How do I post a question effectively?

      understood.

Re^2: min $#a, $#b > $#b
by Eily (Monsignor) on Jan 12, 2016 at 19:57 UTC

    Actually @one in scalar context is the size of the array, $#one is the index of the last element, as shown by the data I print line 12

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1152603]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-10-04 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (18 votes). Check out past polls.

    Notices?