IDL8.4 hard crash [message #90295] |
Wed, 18 February 2015 12:37  |
JDS
Messages: 94 Registered: March 2009
|
Member |
|
|
Try:
IDL> a=hash() & c=(a['b']=hash())
I believe auto-instantiation would make HASH/DICT/etc. much more useful. I.e. I ought to be able to say:
IDL> a=hash() & a['b','c']='test'
and have a['b'] auto-initialized as an empty hash.
JD
|
|
|
|
|
Re: IDL8.4 hard crash [message #90302 is a reply to message #90299] |
Wed, 18 February 2015 13:47   |
JDS
Messages: 94 Registered: March 2009
|
Member |
|
|
>
> The crash is fixed in IDL 8.4.1. Thanks for reporting it!
> -Chris
Thanks.
> p.s. I can't quite wrap my head around how the auto-instantiation would work. More examples?
If you are constructing some nested HASH structure, now you must say:
a=hash()
a['key']=hash()
a['key','sub1']=hash()
a['key','sub1','sub2']=hash()
a['key','sub1','sub2','value']=1.0
This might more typically occur inside a loop pulling keys and values from various locations. This is also by the way why I was using a=hash() & c=(a['b']=hash()) -- to make this sort of construction slightly less painful.
Auto-instantiation means that any hash key which references an undefined value *on assignment* will cause that value to be initialized as a HASH object instead of just saying "key does not exist" and aborting. If that were in place, the above would simply be:
a=hash()
a['key','sub1','sub2','value']=1.0
This comes up quite a bit when attempting to populate deeply nested HASH structures. You end up with code sprinkled with lots of useless tests like:
if ~a[key1].hasKey(key2) then a[key1,key2]=hash()
With auto-instantiation, these statements would be implicit. It also would make Perl programmers happy ;).
Thanks again,
JD
|
|
|
Re: IDL8.4 hard crash [message #90303 is a reply to message #90302] |
Wed, 18 February 2015 14:18   |
chris_torrence@NOSPAM
Messages: 528 Registered: March 2007
|
Senior Member |
|
|
On Wednesday, February 18, 2015 at 2:47:30 PM UTC-7, JDS wrote:
>>
>> The crash is fixed in IDL 8.4.1. Thanks for reporting it!
>> -Chris
>
> Thanks.
>
>> p.s. I can't quite wrap my head around how the auto-instantiation would work. More examples?
>
> If you are constructing some nested HASH structure, now you must say:
>
> a=hash()
> a['key']=hash()
> a['key','sub1']=hash()
> a['key','sub1','sub2']=hash()
> a['key','sub1','sub2','value']=1.0
>
> This might more typically occur inside a loop pulling keys and values from various locations. This is also by the way why I was using a=hash() & c=(a['b']=hash()) -- to make this sort of construction slightly less painful.
>
> Auto-instantiation means that any hash key which references an undefined value *on assignment* will cause that value to be initialized as a HASH object instead of just saying "key does not exist" and aborting. If that were in place, the above would simply be:
>
> a=hash()
> a['key','sub1','sub2','value']=1.0
>
> This comes up quite a bit when attempting to populate deeply nested HASH structures. You end up with code sprinkled with lots of useless tests like:
>
> if ~a[key1].hasKey(key2) then a[key1,key2]=hash()
>
> With auto-instantiation, these statements would be implicit. It also would make Perl programmers happy ;).
>
> Thanks again,
>
> JD
Okay, I like that. I was able to hack it in with 3 lines of code.
IDL> a = hash()
IDL> a['a1','b1','c1','d1','e1','f1','g1','h1'] = 5
IDL> a
{
"a1": {
"b1": {
"c1": {
"d1": {
"e1": {
"f1": {
"g1": {
"h1": 5
}
}
}
}
}
}
}
}
Hmmm. Not sure how I can get the code to you...
-Chris
|
|
|
|
Re: IDL8.4 hard crash [message #90312 is a reply to message #90303] |
Thu, 19 February 2015 00:22   |
Fabzi
Messages: 305 Registered: July 2010
|
Senior Member |
|
|
On 18.02.2015 23:18, Chris Torrence wrote:
> Okay, I like that. I was able to hack it in with 3 lines of code.
>
> IDL> a = hash()
> IDL> a['a1','b1','c1','d1','e1','f1','g1','h1'] = 5
> IDL> a
> {
> "a1": {
> "b1": {
> "c1": {
> "d1": {
> "e1": {
> "f1": {
> "g1": {
> "h1": 5
> }
> }
> }
> }
> }
> }
> }
> }
That's cool! But it doesn't have to be a hash() as default. You could
also need something like:
a = hash(DEFAULT=list())
a['key']->add, 1
Fabien
|
|
|
Re: IDL8.4 hard crash [message #90313 is a reply to message #90312] |
Thu, 19 February 2015 06:40   |
chris_torrence@NOSPAM
Messages: 528 Registered: March 2007
|
Senior Member |
|
|
On Thursday, February 19, 2015 at 1:23:01 AM UTC-7, Fabien wrote:
> On 18.02.2015 23:18, Chris Torrence wrote:
>> Okay, I like that. I was able to hack it in with 3 lines of code.
>>
>> IDL> a = hash()
>> IDL> a['a1','b1','c1','d1','e1','f1','g1','h1'] = 5
>> IDL> a
>> {
>> "a1": {
>> "b1": {
>> "c1": {
>> "d1": {
>> "e1": {
>> "f1": {
>> "g1": {
>> "h1": 5
>> }
>> }
>> }
>> }
>> }
>> }
>> }
>> }
>
> That's cool! But it doesn't have to be a hash() as default. You could
> also need something like:
>
> a = hash(DEFAULT=list())
> a['key']->add, 1
>
> Fabien
But I think in this case, since we're indexing using strings, then we know that we want a hash for the sub-container. I'd hate to complicate it further with a keyword that I have to document.
-Chris
|
|
|
|
Hash auto-instantiation (was IDL8.4 hard crash) [message #90316 is a reply to message #90314] |
Thu, 19 February 2015 10:28  |
chris_torrence@NOSPAM
Messages: 528 Registered: March 2007
|
Senior Member |
|
|
On Thursday, February 19, 2015 at 8:19:42 AM UTC-7, Fabien wrote:
> On 19.02.2015 15:40, Chris Torrence wrote:
>> But I think in this case, since we're indexing using strings,
>> then we know that we want a hash for the sub-container. I'd hate
>> to complicate it further with a keyword that I have to document.
>
> The argument of the string indexes is true if you have more than one
> nested level.
>
> Just in case you are thinking of a Hash() improvement for a future IDL,
> it would be good not to be limited to default hashes only, as does
> python's defaultdict. The roblem of course is that my example is flawed:
>
> a = hash(DEFAULT=list())
>
> is not okay. I shouldn't give an instance of list but rather a "type"
> list or so:
>
> a = hash(DEFAULT='list')
Actually, in my code I am creating the new container based upon my own class. So if you have an IDL Dictionary it will create a Dictionary for the sub-containers. Similarly for the OrderedHash class.
For example:
d = Dictionary()
d['a','b','c'] = 5
help, d['a']
<Expression> DICTIONARY <ID=6216 NELEMENTS=1>
Speaking of dictionaries, now I'm wondering whether it should work for the "dot" notation as well as the brackets:
d = Dictionary()
d.a.b.c = 5 ; should this auto-instantiate???
I'm thinking that it probably should do the same thing...
-Chris
|
|
|