Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file

by FreeBeerReekingMonk (Deacon)
on Jul 15, 2016 at 14:03 UTC ( [id://1167834]=note: print w/replies, xml ) Need Help??


in reply to Re: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file
in thread setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file

Ah, thank you fellow monks for the insight. (actually, I tried tye's way, but it did not work then) Thus:

BEGIN{ unless (defined $ENV{APPLEJACK}){ $ENV{APPLEJACK}= "TRUEBLUE"; exec( $^X, $0, @ARGV ); exit 0; # we actually never get here } } print "STARTING " . $ENV{APPLEJACK} . "\n";

prints:

STARTING TRUEBLUE

Replies are listed 'Best First'.
Re^3: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file
by gravid (Novice) on Jul 18, 2016 at 12:51 UTC

    Thx Everyone.

    The solution you gave indeed work.

    exec( $^X, $0, @ARGV );

    I was sure that whatever written inside a BEGIN statement is hapannig before everything else, so I'm not sure why my first attempt did't wok

    Isn't there a way to solve that without recalling the script again?

    By the way, I don't rely on hash to be sorted, but in this case I did my sort base on the values and not the keys.

    Since the values might be equal I got inconsistent printing while in earlier perl version the printing was consistent.

    Thx

    Guy

      Hi gravid,

      Could you show a short example of the kind of sort you're doing (some sample input data, brief code and expected output)? I'm almost certain there's a better solution than messing with Perl's internal hash settings and re-starting the interpreter.

      Regards,
      -- Hauke D

        Hi,

        Lets say we have this hash:

        $hash{a} = 0; $hash{b} = 1; $hash{c} = 0; $hash{d} = 3;

        The expected output is:

        a = 0 c = 0 b = 1 d = 3

        or

        c = 0 a = 0 b = 1 d = 3

        (note how 'a' & 'c' are switched)

        Both are correct!

        However, for consistency, I rather have always the same output.

        The code that I'm using is:

        foreach my $k ( sort { $hash{$a} <=> $hash{$b} } keys %hash ) { print "$k = $hash{$k}\n"; }

        Thx

        Guy

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-24 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found