Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Passing hash keys to functions

 [1/12] from: premshree::pillai::gmail::com at: 29-Dec-2004 2:35


Hi, I'm trying to write a function that accepts a key as an argument, and then returns the corresponding value. Some code: hsh: make hash! [] append hsh [a [1 2] b [3 4]] get-val: func [of no] [ print hsh/#of/#no ] the get-val function above accepts two arguments. If I pass a and 1 as the argument it should return the value of hsh/a/1. Thoughts? -- Premshree Pillai http://www.livejournal.com/~premshree

 [2/12] from: SunandaDH:aol at: 28-Dec-2004 16:16


Premshree:
> I'm trying to write a function that accepts a key as an argument, and > then returns the corresponding value.
You might be better off with the keys as strings rather than words: hsh: make hash! ["a" [1 2] "b" [3 4]] The code hardly needs a function: select hsh "b" == [3 4] But if you want one: get-val: func [of [hash!] key [string!] ][ return select of key ] get-val hsh "b" == [3 4] Sunanda

 [3/12] from: premshree:pillai:gma:il at: 29-Dec-2004 3:18


On Tue, 28 Dec 2004 16:16:14 EST, [SunandaDH--aol--com] <[SunandaDH--aol--com]> wrote:
> Premshree: > > I'm trying to write a function that accepts a key as an argument, and
<<quoted lines omitted: 4>>
> select hsh "b" > == [3 4]
Ah, I din't know of the existence of a select. :-| I'm a bit confused as to how to use it for keys that have values as hashes, though. If I have something like: hsh: make hash! ["a" [1 2] "b" [3 4]] How do I do something like hsh->"a"->1? TIA
> But if you want one: > get-val: func [of [hash!] key [string!]
<<quoted lines omitted: 7>>
> To unsubscribe from the list, just send an email to rebol-request > at rebol.com with unsubscribe as the subject.
-- Premshree Pillai http://www.livejournal.com/~premshree

 [4/12] from: tomc:darkwing:uoregon at: 28-Dec-2004 14:07


Hi Premshree if 'select returns a series you can just again 'select form that series
>> select select hsh "a" 1
== 2 is that what you were wondering? (where what you are reffering to as a hash could be any series in rebol)
>> h1: ["a" [ 123 456 789] "b" ["foo" "bar" "ack"] "c" "whatever"] >> select select h1 "a" 123
== 456
>> select select h1 "b" "bar"
== "ack"
>> select select h1 "c" "e"
== #"v" On Wed, 29 Dec 2004, Premshree Pillai wrote:

 [5/12] from: premshree:pillai:gm:ail at: 29-Dec-2004 3:41


Oops! I think I screwd up. It works just the way I want it to. Thanks! On Wed, 29 Dec 2004 03:18:10 +0530, Premshree Pillai <[premshree--pillai--gmail--com]> wrote:
> On Tue, 28 Dec 2004 16:16:14 EST, [SunandaDH--aol--com] <[SunandaDH--aol--com]> wrote: > >
<<quoted lines omitted: 37>>
> Premshree Pillai > http://www.livejournal.com/~premshree
-- Premshree Pillai http://www.livejournal.com/~premshree

 [6/12] from: premshree:pillai:g:mail at: 29-Dec-2004 3:45


Yeah, I realised that (as posted in the earlier reply) Thanks! On Tue, 28 Dec 2004 14:07:09 -0800 (PST), Tom Conlin <[tomc--darkwing--uoregon--edu]> wrote:
> Hi Premshree > if 'select returns a series you can just again 'select form that series
<<quoted lines omitted: 66>>
> To unsubscribe from the list, just send an email to rebol-request > at rebol.com with unsubscribe as the subject.
-- Premshree Pillai http://www.livejournal.com/~premshree

 [7/12] from: carl:cybercraft at: 29-Dec-2004 11:52


Hi Premshree, On Wednesday, 29-December-2004 at 3:18:10 Premshree wrote,
>On Tue, 28 Dec 2004 16:16:14 EST, [SunandaDH--aol--com] <[SunandaDH--aol--com]> >wrote:
<<quoted lines omitted: 13>>
>> == [3 4] >Ah, I din't know of the existence of a select. :-|
Incidentally, if you did use words as your keys (as apposed to strings), you wouldn't even need SELECT...
>> hsh: make hash! [a [1 2] b [3 4]]
== make hash! [a [1 2] b [3 4]]
>> hsh/a
== [1 2]
>> hsh/b
== [3 4]
>> hsh/b/1
== 3
>> hsh/b/2
== 4
>> of: 'a no: 1
== 1
>> hsh/:of/:no
== 1
>> of: 'b no: 2
== 2
>> hsh/:of/:no
== 4 I'm not sure why Sunanda suggested using strings, but I'm sure there's a good reason - possibly because of the limit to the number of words allowed in REBOL. Or perhaps for speed reasons. Sunanda? -- Carl Read.

 [8/12] from: SunandaDH::aol::com at: 29-Dec-2004 2:47


Carl:
> I'm not sure why Sunanda suggested using strings, but I'm sure there's a
good
> reason - possibly because of the limit to the number of words allowed in > REBOL. Or perhaps for speed reasons. Sunanda?
I assumed -- possibly wrongly -- that using words rather than strings was a coding error. The limit on total words will be one problem with this approach, as you say. (Depending on the version of REBOL here is an upper limit of around 8192 differently spelled words. I'm not sure there is a limit on the number of different words of the same spelling that can exist) Another problem will be if the data was being read from a file. The keys may not all be in a format that easily converts to words -- perhaps they have embedded spaces, or characters that don't allow you to use the word in open code. (Yes, I know a word can have embedded spaces: blk: copy [] append blk to-word "x x" but you'd be adding a lot of extra code to handle words like that. One example: it doesn't lead to blocks you can easily mold and load again) Sunanda.

 [9/12] from: premshree::pillai::gmail::com at: 29-Dec-2004 16:25


On Wed, 29 Dec 2004 20:31:15 +1300, Carl Read <[carl--cybercraft--co--nz]> wrote:
> Hi Premshree, > On Wednesday, 29-December-2004 at 3:18:10 Premshree wrote,
<<quoted lines omitted: 35>>
> >> hsh/:of/:no > == 4
This was what I was using initially. But I need to use this within a function, and the key is taken as an argument, so I cannot use this method. Or can I?
> I'm not sure why Sunanda suggested using strings, but I'm sure there's a good reason - possibly because of the limit to the number of words allowed in REBOL. Or perhaps for speed reasons. Sunanda? > -- Carl Read.
<<quoted lines omitted: 5>>
> To unsubscribe from the list, just send an email to rebol-request > at rebol.com with unsubscribe as the subject.
-- Premshree Pillai http://www.livejournal.com/~premshree

 [10/12] from: lmecir:mbox:vol:cz at: 29-Dec-2004 12:07


Premshree Pillai napsal(a):
>Hi, >I'm trying to write a function that accepts a key as an argument, and
<<quoted lines omitted: 8>>
>the argument it should return the value of hsh/a/1. >Thoughts?
See http://www.compkarori.com/vanilla/display/aa.r , it may be of some use. -Ladislav

 [11/12] from: greggirwin:mindspring at: 29-Dec-2004 12:11


PP> This was what I was using initially. But I need to use this within a PP> function, and the key is taken as an argument, so I cannot use this PP> method. Or can I? Sure you can. And you can use a lot of datatypes for keys. You don't want to use numbers (integer! and decimal!) because those will be interpreted as positions (e.g. for use with PICK), not keys. my-select: func [series [series!] key] [ attempt [series/:key] ] hash-ser: make hash! [<a> [1 2] <b> [3 4]] hash-ser/<a> my-select hash-ser <a> series: [#"z" $2.2 3x3 a <b> #c %file-d [name--host--dom] http://www.test.url #"z"] foreach key series [ print [key tab series/:key] ] -- Gregg

 [12/12] from: premshree::pillai::gmail::com at: 30-Dec-2004 1:27


On Wed, 29 Dec 2004 12:11:27 -0700, Gregg Irwin <[greggirwin--mindspring--com]> wrote:
> PP> This was what I was using initially. But I need to use this within a > PP> function, and the key is taken as an argument, so I cannot use this
<<quoted lines omitted: 5>>
> attempt [series/:key] > ]
Cool. I think I'm getting the hand of things. Thanks!
> hash-ser: make hash! [<a> [1 2] <b> [3 4]] > hash-ser/<a>
<<quoted lines omitted: 7>>
> To unsubscribe from the list, just send an email to rebol-request > at rebol.com with unsubscribe as the subject.
-- Premshree Pillai http://www.livejournal.com/~premshree

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted