in reply to Re: DBI and mysql query formatting
in thread DBI and mysql query formatting

"... this will ensure you always have the same order: my @keys = keys %test;"

Actually, that's only true for versions of Perl prior to 5.18.0. From perl5180delta: Hash overhaul:

"By default, two distinct hash variables with identical keys and values may now provide their contents in a different order where it was previously identical."

I have v5.18.1 - here's a few example runs:

$ perl -Mstrict -Mwarnings -E 'my %x = map { $_ => 1 } "A" .. "E"; my +@y = keys %x; say "@y"' A D E C B $ perl -Mstrict -Mwarnings -E 'my %x = map { $_ => 1 } "A" .. "E"; my +@y = keys %x; say "@y"' D E C B A $ perl -Mstrict -Mwarnings -E 'my %x = map { $_ => 1 } "A" .. "E"; my +@y = keys %x; say "@y"' D E B A C

Either a sort on the keys, or even a hard-coded list, would probably provide a more robust solution.

-- Ken

Replies are listed 'Best First'.
Re^3: DBI and mysql query formatting
by tangent (Parson) on Dec 06, 2013 at 11:20 UTC
    Yes, I agree. But the goal of the OP is to have the same order for the $str and $val, not for every run.
      But the goal of the OP is to have the same order for the $str and $val, not for every run.

      That's what values guarantees:

      [...] So long as a given hash is unmodified you may rely on keys, values and each to repeatedly return the same order as each other. [...]

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Ah, like the OP I was never sure about that and had not come across the guarantee you quote.

        I would go with the more complete code you have posted below.