zsh-users
 help / color / mirror / code / Atom feed
* Keying arrays to names: is there an array of arrays?
@ 2006-05-26 14:20 Johann 'Myrkraverk' Oskarsson
  2006-05-26 15:26 ` Marc Chantreux
  0 siblings, 1 reply; 9+ messages in thread
From: Johann 'Myrkraverk' Oskarsson @ 2006-05-26 14:20 UTC (permalink / raw)
  To: zsh-users

Hi,

First, I'd like to thank the zsh developers for a wonderful shell.

Second, I'd like to ask if it's possible to key an array to a name?

I tried to create an associative array and assign it a key + regular
array, but that doesn't seem to work.

That is:

  typeset -A array
  array[key]=(lots of values here)

doesn't work.

Is there some other way I can use to achive the same thing?


Johann

-- 
johann myrkraverk com (you know the drill with the @ and .)
I classify Outlook mail as spam, use something else.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Keying arrays to names: is there an array of arrays?
  2006-05-26 14:20 Keying arrays to names: is there an array of arrays? Johann 'Myrkraverk' Oskarsson
@ 2006-05-26 15:26 ` Marc Chantreux
  2006-05-26 15:40   ` Peter Stephenson
  0 siblings, 1 reply; 9+ messages in thread
From: Marc Chantreux @ 2006-05-26 15:26 UTC (permalink / raw)
  To: zsh-users

Hi,

> Second, I'd like to ask if it's possible to key an array to a name?

% typeset -A a
% a[a]='those are values'
% print -l ${(s: :)a[a]}
those
are
values


regards


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Keying arrays to names: is there an array of arrays?
  2006-05-26 15:26 ` Marc Chantreux
@ 2006-05-26 15:40   ` Peter Stephenson
  2006-05-26 15:58     ` Peter Stephenson
  2006-05-27 19:09     ` Johann 'Myrkraverk' Oskarsson
  0 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 2006-05-26 15:40 UTC (permalink / raw)
  To: zsh-users

Marc Chantreux wrote:
> > Second, I'd like to ask if it's possible to key an array to a name?
>
> % typeset -A a
> % a[a]='those are values'
> % print -l ${(s: :)a[a]}
> those
> are
> values

This reminds me I was going to post a general solution along the same
lines which deals with embeded spaces etc.:

% array=(one 'two three' '"four five six"')
% typeset -A hash
% array=(${(q)array})
% hash[key]="${array}"
% array=(${(Q)${(z)hash[key]}})
% print -r $array
one
two three
"four five six"

I couldn't work out how to do the encoding in a single step,
since "${(q)array}" and all the variants of it I could think of quote
the spaces that are to be used as separators.

--
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Keying arrays to names: is there an array of arrays?
  2006-05-26 15:40   ` Peter Stephenson
@ 2006-05-26 15:58     ` Peter Stephenson
  2006-05-27 19:09     ` Johann 'Myrkraverk' Oskarsson
  1 sibling, 0 replies; 9+ messages in thread
From: Peter Stephenson @ 2006-05-26 15:58 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson wrote:
> % array=(one 'two three' '"four five six"')
> % typeset -A hash
> % array=(${(q)array})
> % hash[key]="${array}"
> % array=(${(Q)${(z)hash[key]}})
> % print -r $array
          ^^ Sorry, should be -rl for the output shown.
> one
> two three
> "four five six"

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Keying arrays to names: is there an array of arrays?
  2006-05-26 15:40   ` Peter Stephenson
  2006-05-26 15:58     ` Peter Stephenson
@ 2006-05-27 19:09     ` Johann 'Myrkraverk' Oskarsson
  2006-05-27 22:10       ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Johann 'Myrkraverk' Oskarsson @ 2006-05-27 19:09 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson <pws@csr.com> writes:

> This reminds me I was going to post a general solution along the same
> lines which deals with embeded spaces etc.:

Is it possible to abstract that trick into functions?  That is, so the
function takes the array name, key, and value as arguments?  My feeble
attempts have failed.

> % array=(one 'two three' '"four five six"')
> % typeset -A hash
> % array=(${(q)array})
> % hash[key]="${array}"
> % array=(${(Q)${(z)hash[key]}})
> % print -rl $array
> one
> two three
> "four five six"


Johann

-- 
johann myrkraverk com (you know the drill with the @ and .)
I classify Outlook mail as spam, use something else.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Keying arrays to names: is there an array of arrays?
  2006-05-27 19:09     ` Johann 'Myrkraverk' Oskarsson
@ 2006-05-27 22:10       ` Bart Schaefer
  2006-05-28  2:34         ` Johann 'Myrkraverk' Oskarsson
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2006-05-27 22:10 UTC (permalink / raw)
  To: zsh-users

On May 27,  7:09pm, Johann 'Myrkraverk' Oskarsson wrote:
}
} > This reminds me I was going to post a general solution along the same
} > lines which deals with embeded spaces etc.:
} 
} Is it possible to abstract that trick into functions?  That is, so the
} function takes the array name, key, and value as arguments?

I'll assume you mean the *associative* array (hash) name.

A function to do the assignment isn't too terrible, though there are
scoping issues -- either the hash already has to exist, or you can
only create it in the global scope by using typeset -g:

  setbykey() {
    local h=$1 k=$2
    shift 2
    set -- ${(q)*}
    typeset -gA $h
    typeset -g $h\[$k\]="$*"
  }

A function to read back the value is a bit trickier.  What do you want
to do with the array once you have it?  A common fallback would be to
stuff it in $reply:

  getbykey() {
    set -- $1\[$2\]
    reply=( ${(Q)${(z)${(P)1}}} )
  }

  typeset -A hash
  setbykey hash key one 'two three' '"four five six"'
  getbykey hash key
  print -rl $reply

Incidentally:

} I classify Outlook mail as spam, use something else.

Someone asking for assistance has no right to dictate terms.  You want
my help, you take it the way I send it.  I don't happen to use Outlook,
but next time I see something like this, I just won't answer.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Keying arrays to names: is there an array of arrays?
  2006-05-27 22:10       ` Bart Schaefer
@ 2006-05-28  2:34         ` Johann 'Myrkraverk' Oskarsson
  2006-05-28 18:17           ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Johann 'Myrkraverk' Oskarsson @ 2006-05-28  2:34 UTC (permalink / raw)
  To: ZSH Users

Bart Schaefer <schaefer@brasslantern.com> writes:

> I'll assume you mean the *associative* array (hash) name.

Yes, thank you very much.

> A function to do the assignment isn't too terrible, though there are
> scoping issues -- either the hash already has to exist, or you can
> only create it in the global scope by using typeset -g:

The following version also works with IFS="" (I guess it will work
with IFS equal to something sensible too).

mapassign() {
    local h=$1 k=$2 
    shift 2 
    set -- ${(j: :q)*} 
    typeset -gA $h 
    typeset -g $h\[$k\]="$*"
}

> A function to read back the value is a bit trickier.  What do you want
> to do with the array once you have it?  A common fallback would be to
> stuff it in $reply:

Same with this one:

mapread() {
    set -- $1\[$2\]
    reply=( ${(s: :Q)${(z)${(P)1}}} ) 
}

> Incidentally:
>
> } I classify Outlook mail as spam, use something else.
>
> Someone asking for assistance has no right to dictate terms.  You want
> my help, you take it the way I send it.  I don't happen to use Outlook,
> but next time I see something like this, I just won't answer.

That isn't meant for mailing lists, but for private messages.  I've
modified my tag line a bit, but it's also your prerogative not to
answer.


-- 
johann myrkraverk com (you know the drill with the @ and .)
I classify Outlook mail as spam, please use something else for private
messages.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Keying arrays to names: is there an array of arrays?
  2006-05-28  2:34         ` Johann 'Myrkraverk' Oskarsson
@ 2006-05-28 18:17           ` Bart Schaefer
  2006-05-28 21:32             ` Johann 'Myrkraverk' Oskarsson
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2006-05-28 18:17 UTC (permalink / raw)
  To: ZSH Users

On May 28,  2:34am, Johann 'Myrkraverk' Oskarsson wrote:
>
> The following version also works with IFS="" (I guess it will work
> with IFS equal to something sensible too).
> 
> mapassign() {
>     local h=$1 k=$2 
>     shift 2 
>     set -- ${(j: :q)*} 
>     typeset -gA $h 
>     typeset -g $h\[$k\]="$*"
> }

A better approach might be:

    mapassign() {
        emulate -L zsh
	local h=$1 k=$2 IFS=$' \t\n\0'
	shift 2
	set -- ${(q)*}
	typeset -gA $h
	typeset -g $h\[$k\]="$*"
    }
 
> Same with this one:
> 
> mapread() {
>     set -- $1\[$2\]
>     reply=( ${(s: :Q)${(z)${(P)1}}} ) 
> }

That's actually incorrect.  The (z) option is already splitting the
value; you shouldn't need or want to split it again with (s: :).  If
the setting of IFS is preventing (z) from working properly, that's
probably a bug, but in any case the workaround is to make IFS local.

    mapread() {
	emulate -L zsh
	local IFS=$' \t\n\0'
	set -- $1\[$2\]
	reply=( ${(Q)${(z)${(P)1}}} )
    }

If you're going to use these functions for hash access anyway, then
you might as well also use ${(q)2} instead of $2 in both of them, to
avoid issues with non-alphanumeric keys.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Keying arrays to names: is there an array of arrays?
  2006-05-28 18:17           ` Bart Schaefer
@ 2006-05-28 21:32             ` Johann 'Myrkraverk' Oskarsson
  0 siblings, 0 replies; 9+ messages in thread
From: Johann 'Myrkraverk' Oskarsson @ 2006-05-28 21:32 UTC (permalink / raw)
  To: ZSH Users

Bart Schaefer <schaefer@brasslantern.com> writes:

> That's actually incorrect.  The (z) option is already splitting the
> value; you shouldn't need or want to split it again with (s: :).  If
> the setting of IFS is preventing (z) from working properly, that's
> probably a bug, but in any case the workaround is to make IFS local.

Well, all *I* know is that the s: : is needed -- at least without the
local IFS solution.  If someone can (and is willing) to convince me
that's a bug (I don't yet understand all the flags involved in these
expansions) I can cook up some nice test case/bug report.

Thank you for the local IFS, that does look/appear to be cleaner.

> If you're going to use these functions for hash access anyway, then
> you might as well also use ${(q)2} instead of $2 in both of them, to
> avoid issues with non-alphanumeric keys.

Thank you.  I'll keep that in mind if I run into the problem (dunno,
if I will, I'm prototyping an app and the keys will be filenames).


Johann

-- 
johann myrkraverk com (you know the drill with the @ and .)
I classify Outlook mail as spam, please use something else for
private messages.


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2006-05-28 21:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-26 14:20 Keying arrays to names: is there an array of arrays? Johann 'Myrkraverk' Oskarsson
2006-05-26 15:26 ` Marc Chantreux
2006-05-26 15:40   ` Peter Stephenson
2006-05-26 15:58     ` Peter Stephenson
2006-05-27 19:09     ` Johann 'Myrkraverk' Oskarsson
2006-05-27 22:10       ` Bart Schaefer
2006-05-28  2:34         ` Johann 'Myrkraverk' Oskarsson
2006-05-28 18:17           ` Bart Schaefer
2006-05-28 21:32             ` Johann 'Myrkraverk' Oskarsson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).