Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^7: index of the minimum element of the array

by oiskuu (Pilgrim)
on Jan 17, 2014 at 19:41 UTC ( #1071011=note: print w/ replies, xml ) Need Help??


in reply to Re^6: index of the minimum element of the array
in thread index of the minimum element of the array

Fair enough. However, the sub is called with inherited @_ and it happens to work.

Same thing with map:

sub foo { map { print $_[$_] } 0 .. $#_ }
Can I not rely on this behavior?

Update. example code:

use List::Util 'reduce'; sub minindex { reduce { $_[$a] < $_[$b] ? $a : $b } 0 .. $#_ } my @g = (55, 88, 33, 6, 234, 234, 52, 6, 1324, 22, 1234); print minindex @g; # $g[7] == 6
This is perl 5, version 12, subversion 3 (v5.12.3) built for x86_64-linux-thread-multi
List::Util $VERSION = "1.22";

Update2. One final observation:
I also see coderef passed to the sub when run under debugger. But not when run normally.


Comment on Re^7: index of the minimum element of the array
Select or Download Code
Re^8: index of the minimum element of the array
by LanX (Canon) on Jan 17, 2014 at 19:53 UTC
    map and grep are very different beasts, cause the block is NOT handled like an anonymous sub but more like a do-block.

    DB<117> sub tst { map {$_[$_]+1} 0..3 } DB<118> tst 1..10 => (2, 3, 4, 5)

    But your code with reduce didn't work for me, it always returned 0 cause thats the smallest index you passed. 1 cause ...

    Could you show us an example and tell us which versions you use?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    ) @_=(BLOCKREF, indices)

    DB<148> @a= map { int rand 100 } 1..5 => (32, 4, 90, 79, 70) DB<149> sub minindex { reduce { print "@_\t$a:$_[$a],$b:$_[$b]\n"; $ +_[$a] < $_[$b] ? $a : $b } 0 .. $#_ } DB<150> minindex @a CODE(0xa353ea8) 0 1 2 3 4 0:CODE(0xa353ea8),1:0 CODE(0xa353ea8) 0 1 2 3 4 1:0,2:1 CODE(0xa353ea8) 0 1 2 3 4 1:0,3:2 CODE(0xa353ea8) 0 1 2 3 4 1:0,4:3 => 1
Re^8: index of the minimum element of the array
by LanX (Canon) on Jan 17, 2014 at 20:23 UTC
    re update:

    thats the interesting part: )

    DB<102> $List::Util::VERSION => "1.22"

    and shouldn't your code return 3 (the first 6) ?

    update

    the result is correct, your code produces the last min.

    update

    you have the same version of List::Util? strange!

    perl -v This is perl, v5.10.0 built for i486-linux-gnu-thread-multi

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re^8: index of the minimum element of the array
by ikegami (Pope) on Jan 20, 2014 at 15:32 UTC

    For reduce, no. It's not documented. It's a side-effect of the implementation you tested.

    For map, yes. map is a not a subroutine; it's an operator. It's not constrained to the syntax and convention of subroutine calls Its argumentoperand is not a sub.

    I also see coderef passed to the sub when run under debugger. But not when run normally.

    It's easy to see by using return inside the block. It's also easy to see using -MO=Concise.

    $ perl -MList::Util=reduce -MO=Concise,-exec -e'reduce { "..." } @a' ... 4 <0> pushmark sRM 5 <$> anoncode[CV ] lRM 6 <1> refgen KM/1 7 <#> gv[*a] s 8 <1> rv2av[t4] lKM/1 9 <#> gv[*reduce] s a <1> entersub[t5] vKS/TARG,1 ... {15} eric@dev-eric [~/work/p4/src/main/CC] $ perl -MList::Util=reduce -MO=Concise,-exec -e'&reduce(sub { "..." }, + @a)' ... 4 <0> pushmark sRM 5 <$> anoncode[CV ] lRM 6 <1> refgen lKM/1 7 <#> gv[*a] s 8 <1> rv2av[t4] lKM/1 9 <#> gv[*reduce] s a <1> entersub[t5] vKS/TARG,AMPER,1 ...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (17)
As of 2014-07-29 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (218 votes), past polls