Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Add a fixed number of unique elements to hash

by tybalt89 (Monsignor)
on Mar 05, 2023 at 21:11 UTC ( [id://11150762]=note: print w/replies, xml ) Need Help??


in reply to Re: Add a fixed number of unique elements to hash
in thread Add a fixed number of unique elements to hash

There is also this that does the equivalent of shuffle and pick N.

use List::AllUtils qw( sample ); my @selected = sample $numOfRandFiles, @audioFiles;

Replies are listed 'Best First'.
Re^3: Add a fixed number of unique elements to hash [sample() vs. shuffle()]
by kcott (Archbishop) on Mar 05, 2023 at 22:20 UTC

    G'day tybalt89,

    When initially writing the code in my response, I checked the List::Util::shuffle() documentation and noticed the sample() function immediately after it. I hadn't encountered that previously and decided to give it a go.

    Curiously, although it did work as documented, multiple runs produced the same results. As you can see from my "sample runs" using shuffle(), multiple runs produced different results (yes, only two runs shown, but I did run it quite a few times).

    Looking at source/lib/List/AllUtils.pm, List::AllUtils::sample() should be identical to List::Util::sample(). I have List::Util v1.62 and Perl v5.36.0.

    I'm a bit short on time this morning; I was thinking of investigating further this afternoon [Aussie timezone: UTC+11:00]. If you have any insights into the behaviour of sample(), please share.

    It did seem like sample(), taking a random selection from the array, was probably a better choice than shuffle(), randomising the entire array and then taking a slice (obviously, benchmarking needed to confirm this). On the down side, sample() requires List::Util v1.54: you'd need at least Perl v5.32.0 (which has v1.55) or an upgrade from CPAN.

    — Ken

      I have a slightly newer verison of List::Util here (1.63 as below) and sample appears to work as intended, ie. different runs produce different results:

      $ pmv List::Util 1.63 $ perl -MList::Util=sample -wE 'my @x=("a".."z"); say sample (3, @x)' jso $ perl -MList::Util=sample -wE 'my @x=("a".."z"); say sample (3, @x)' ter $ perl -MList::Util=sample -wE 'my @x=("a".."z"); say sample (3, @x)' mgl $ perl -MList::Util=sample -wE 'my @x=("a".."z"); say sample (3, @x)' ago $

      There's nothing in the List::Util changelog to indicate any modification to sample so either some other fix has had an effect or something else is at play.


      🦛

        G'day hippo,

        ++ Thanks for checking that out. I ran a verbatim copy of your one-liner four times and got a different result each time; so, on the face of it, this seems to be working correctly (with v1.62 & v1.63).

        I did actually sit down to run some tests yesterday; unfortunately, real-life barged in and the testing needed to take a backseat. I will get to it eventually.

        I'm fairly certain that I didn't change any logic when moving from sample to shuffle. I recall only a very minor modification; probably just these two lines (off the top of my head):

        #use List::Util 'sample'; use List::Util 'shuffle'; ... #my @playlist = sample $list_length, @all_files; my @playlist = (shuffle @all_files)[0 .. $list_length - 1];

        I'll post more when I get to the testing. It's 11pm here now, so that won't be tonight. :-)

        — Ken

      Many thanks for your replies and testing, but the target machine is running Perl 5.004 (unfortunately I wasn't precise enough when I just wrote Perl 5). The final solution was to use splice as suggested by jwkrahn in the next post.

        What kind of machine are you running Perl 5.004 on?

        Seriously, you've peaqued my curiosity. You have some old desktop system from 1998 up and running? Anything newer than that, you would have upgraded to a more modern version of Perl long ago. Perl 5.004 is 25 years old.


        Dave

Re^3: Add a fixed number of unique elements to hash
by jo37 (Deacon) on Mar 06, 2023 at 20:24 UTC

    Be careful with List::AllUtils. "All" isn't really "all". At least frequency from List::MoreUtils is missing. I prefer to use the original modules.

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

      Probably because of

      use List::AllUtils qw( count_by ); %frequencies = count_by { $_ } LIST;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-14 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found