Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:57:10+00:00 2026-05-27T12:57:10+00:00

While trying to understand how refp’s solution for random hash value selection works, I

  • 0

While trying to understand how refp’s solution for random hash value selection works, I noticed something strange.

With repeated calls to the following Perl script, I consistently found that the first result returned was the same. The subsequent values returned were random:

use strict;
use warnings;
use 5.010;

my %hash = map { $_ => ord $_ } 'a' .. 'z';

say( (@_=%hash)[1|rand@_] ) for 1 .. 10;       # First value always 119

Interestingly, the following does not suffer from this issue:

sub random_value { ( @_ )[ 1 | rand @_ ] }

say random_value %hash for 1 .. 10;            # First value is random

Removing the references to @_ also remedies the problem:

say( (%hash)[1|rand keys %hash] ) for 1 .. 10; # First value is random

This has been tested on Windows (ActivePerl 5.14.2).

On the surface, it looks like setting @_ has something to do with it, but I’m not sure. Can anyone shed some light on what’s happening here?


EDIT

I thought this question was answered until refp provided an update. Why does the arrayref form not suffer from the same issue discussed above? :

[@_=%hash]->[1|rand@_] for 1 .. 10;            # First value is random
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:57:11+00:00Added an answer on May 27, 2026 at 12:57 pm

    I suspect there is a race condition where @_ is not defined in the first loop iteration.

    say( (@_=%hash)[1|rand@_] ) for 1 .. 10; 
    

    Will become

    say( (@_=%hash)[1|rand ()] ) for 1 .. 10; 
    

    It escapes warnings because @_ is a predeclared variable. As you will notice:

    say( (my @a=%hash)[1|rand@a] ) for 1 .. 10;   
    

    Will crash and burn because @a is not defined in the postscript.

    Update:

    [@_=%hash]->[1|rand@_] for 1 .. 10;    
    

    Is not any different. It is still bad practice to use a variable in the same statement that you assign it. The difference, I am guessing, is that the precedence is somewhat altered, so that the assignment is evaluated first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand how a while loop looks in IL. I have written
While trying to understand how a web server worked, I came accross this: //myfile.js
I am having some difficulties while trying to understand the radio buttons logic. I
I have a legacy code to maintain and while trying to understand the logic
While trying to do something a bit more complicated, I ran across a behavior
While trying to experiment with copy construction by 'pass by value' and the followed
I'm having some difficulties while trying to understand the whole zend routing concept. My
I am trying to understand this code snippet: while row = input.gets row.strip! next
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
I face a strange problem while I'm trying to document a project of mine.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.