[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Order of element in associative array expansion

From: Davide Brini
Subject: Re: [Help-bash] Order of element in associative array expansion
Date: Fri, 18 Oct 2013 10:33:08 +0200

On Thu, 17 Oct 2013 15:37:09 -0400, Greg Wooledge <address@hidden>

> > On 10/17/13 12:23 PM, Davide Brini wrote:
> > > I know that the order of keys and values obtained by expanding an
> > > associative array with "address@hidden" and "address@hidden" respectively 
> > > is
> > > unpredictable, which is fine.
> > > 
> > > However my question is: at a given point in time, without changes or
> > > other operations happening in between, are the two expansions
> > > guaranteed to return the keys in the same order as the respective
> > > values and viceversa?
> I can't resist asking: why does it matter?  Under what circumstances
> would you traverse these two lists independently, but wish to have them
> match up properly?
> Obfuscated programming contest?

Not really. A question from a friend got me thinking...

It could be a simple-minded way to serialize a bash hash and pass it to
another program or script. Simple example (pseudocode):

someotherscript "address@hidden" "address@hidden"

and in the other script you could do

for (i = 1; i <= argc / 2; i++)
  hash[argv[i]] = argv[i + argc / 2 ]

and at the end you have rebuilt the hash inside the other program. However,
this requires that the expansion of the keys and the values happen in the
same order, which is what I asked.

I'm not saying this is an especially good/optimal/whatever way to do
things and I'm perfectly aware a zillion of other ways exist, so let's
leave this aside; I've just striclty answered the question.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]