autovivification

PHP autovivification

跟風遠走 提交于 2019-12-04 03:53:32
Update: My original intention for this question was to determine if PHP actually has this feature. This has been lost in the answers' focus on the scalar issue. Please see this new question instead: "Does PHP have autovivification?" This question is left here for reference. According to Wikipedia , PHP doesn't have autovivification, yet this code works: $test['a']['b'] = 1; $test['a']['c'] = 1; $test['b']['b'] = 1; $test['b']['c'] = 1; var_dump($test); Output: array 'a' => array 'b' => int 1 'c' => int 1 'b' => array 'b' => int 1 'c' => int 1 I found that this code works too: $test['a'][4] = 1

Does PHP have autovivification?

折月煮酒 提交于 2019-12-03 22:49:16
Searching PHP.net for autovivification gives no results. At the time of writing, Wikipedia claims that only Perl has it. There are no clearly definitive results when searching Google for "php autovivification". This PHP code runs fine: $test['a'][4][6]['b'] = "hello world"; var_dump($test); array 'a' => array 4 => array 'b' => array ... Can anyone provide a canonical answer (preferably with references) that PHP does have this feature, and any details such as the version it was introduced in, quirks, shortcuts etc? Yes, PHP does have autovivification (and has had it for a long time), although

How to change behavior of dict() for an instance

↘锁芯ラ 提交于 2019-12-02 14:56:58
So I'm writing a class that extends a dictionary which right now uses a method "dictify" to transform itself into a dict. What I would like to do instead though is change it so that calling dict() on the object results in the same behavior, but I don't know which method to override. Is this not possible, or I am I missing something totally obvious? (And yes, I know the code below doesn't work but I hope it illustrates what I'm trying to do.) from collections import defaultdict class RecursiveDict(defaultdict): ''' A recursive default dict. >>> a = RecursiveDict() >>> a[1][2][3] = 4 >>> a

problem loading Autovivification file when moving from python 2.7 to 3.6, KeyError: 'DictType'

倾然丶 夕夏残阳落幕 提交于 2019-12-02 11:43:13
问题 In python 2.7, I have a bunch of files stored as the following object: class AutoVivification(dict): """Implementation of perl's autovivification feature.""" def __getitem__(self, item): try: return dict.__getitem__(self, item) except KeyError: value = self[item] = type(self)() return value This is from What is the best way to implement nested dictionaries?. I have them pickled and can load them just fine. But in python 3.6, it gives me the following error when trying to load the same file.

Multiple initialization of auto-vivifying hashes using a new operator in Ruby

我是研究僧i 提交于 2019-12-02 11:21:02
问题 I would like to initialize several auto-vivifying hashes by one-line expression. So far I came to an extra method for the AutoHash object: class AutoHash < Hash ... def few(n=0) Array.new(n) { AutoHash.new } end which allows me to do the following a, b, c = AutoHash.new.few 3 However, I feel that one can make the following sentence possible by defining a new operator := a := b := c = AutoHash.new Could you help me to implement this? Do I have to use superators? require 'superators' class

Python: How to update value of key value pair in nested dictionary?

有些话、适合烂在心里 提交于 2019-12-02 05:30:00
问题 i am trying to make an inversed document index, therefore i need to know from all unique words in a collection in which doc they occur and how often. i have used this answer in order two create a nested dictionary. The provided solution works fine, with one problem though. First i open the file and make a list of unique words. These unique words i than want to compare with the original file. When there is a match, the frequency counter should be updated and its value be stored in the two

problem loading Autovivification file when moving from python 2.7 to 3.6, KeyError: 'DictType'

本小妞迷上赌 提交于 2019-12-02 04:31:52
In python 2.7, I have a bunch of files stored as the following object: class AutoVivification(dict): """Implementation of perl's autovivification feature.""" def __getitem__(self, item): try: return dict.__getitem__(self, item) except KeyError: value = self[item] = type(self)() return value This is from What is the best way to implement nested dictionaries? . I have them pickled and can load them just fine. But in python 3.6, it gives me the following error when trying to load the same file. Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1

Python: How to update value of key value pair in nested dictionary?

淺唱寂寞╮ 提交于 2019-12-02 02:24:43
i am trying to make an inversed document index, therefore i need to know from all unique words in a collection in which doc they occur and how often. i have used this answer in order two create a nested dictionary. The provided solution works fine, with one problem though. First i open the file and make a list of unique words. These unique words i than want to compare with the original file. When there is a match, the frequency counter should be updated and its value be stored in the two dimensional array. output should eventually look like this: word1, {doc1 : freq}, {doc2 : freq} <br> word2,

How can I access a deeply nested dictionary using tuples?

让人想犯罪 __ 提交于 2019-12-01 05:35:10
I would like to expand on the autovivification example given in a previous answer from nosklo to allow dictionary access by tuple. nosklo's solution looks like this: class AutoVivification(dict): """Implementation of perl's autovivification feature.""" def __getitem__(self, item): try: return dict.__getitem__(self, item) except KeyError: value = self[item] = type(self)() return value Testing: a = AutoVivification() a[1][2][3] = 4 a[1][3][3] = 5 a[1][2]['test'] = 6 print a Output: {1: {2: {'test': 6, 3: 4}, 3: {3: 5}}} I have a case where I want to set a node given some arbitrary tuple of

Is there a library to support autovivification on Javascript objects?

非 Y 不嫁゛ 提交于 2019-12-01 04:21:10
Is there anyway, either natively or through a library, to use autovivification on Javascript objects? IE, assuming foo is an object with no properties, being able to just do foo.bar.baz = 5 rather than needing foo.bar = {}; foo.bar.baz = 5 . You can't do it exactly with the syntax you want. But as usual, in JS you can write your own function: function set (obj,keys,val) { for (var i=0;i<keys.length;i++) { var k = keys[i]; if (typeof obj[k] == 'undefined') { obj[k] = {}; } obj = obj[k]; } obj = val; } so now you can do this: // as per you example: set(foo,['bar','baz'],5); without worrying if