Questions tagged [autovivification]

Autovivification means implicitly creating data structures accessed via name when explicitly creating their data, such as initializing a hash upon assigning the first key/value pair, or creating a folder upon saving a file in a new path.

75 questions
4
votes
1 answer

perl autovivification when calling subroutine

Why auto-vivification does not work when calling procedures? Is there a way to prohibit it in this case too? #!/usr/bin/env perl no autovivification; use Data::Dumper; sub testsub { } my $task; print Dumper($task); # $VAR1 = undef; my $a =…
4
votes
3 answers

Unintentionally adding keys to hash while iterating

I'm iterating through a cache of a hash of hashes of latitude keys that point to key/value pairs of longitudes/cities. I'm trying to find approximate matches for latitudes/longitudes that are close enough to what's already been looked up and is in…
ikebukuru
  • 89
  • 7
4
votes
1 answer

Perl cgi compilation error in autovivication.pm

I'm using a perl cgi script that uses our own libraries, which use the "no autovivification" pragma. E.g. /usr/lib/company/mysim.cgi: #!/usr/bin/perl -w use strict; # ... other use use Company::Module1; /usr/lib/perl5/Company/Module1.pm package…
4
votes
3 answers

PERL-like autovivification with default value in Python, and returns a default value from non-existing arbitrary nesting?

Suppose I want PERL-like autovivication in Python, i.e.: >>> d = Autovivifier() >>> d = ['nested']['key']['value']=10 >>> d {'nested': {'key': {'value': 10}}} There are a couple of dominant ways to do that: Use a recursive default dict Use a…
dawg
  • 98,345
  • 23
  • 131
  • 206
4
votes
4 answers

Why does Perl autovivify in this case?

Why does $a become an arrayref? I'm not pushing anything to it. perl -MData::Dumper -e 'use strict; 1 for @$a; print Dumper $a' $VAR1 = [];
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
3
votes
2 answers

Why does $foo->{bar} autovivify but %$foo doesn't?

I have the following code: $headers; some_sub( %$headers ); When I call some_sub I get an error: Can't use an undefined value as a HASH reference at ... But similar code does not produce an error: $headers->{ x }; Why doesn't autovivification…
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
3
votes
4 answers

How to create nested dictionaries with duplicate keys in python

I want to create data structure with nested dictionaries and duplicate keys. A detailed example is: data['State1']['Landon']['abc Area'] = 'BOB' data['State1']['Landon']['abc Area'] = 'SAM' data['State1']['Landon']['xyz Area'] =…
Aniketan
  • 55
  • 2
  • 9
3
votes
4 answers

Why does Perl's autovivification work in this case?

Can some one help me understand the output of this Perl program: use Data::Dumper; my %hash; $hash{hello} = "foo"; $hash{hello}{world} = "bar"; print $hash{hello} . "\n"; print $hash{hello}{world} . "\n"; print Dumper(\%hash); And the…
tster
  • 17,883
  • 5
  • 53
  • 72
3
votes
5 answers

In Perl, how to use 'defined' function on elements of two-dimensional array?

I am trying to check if an element is defined, using defined function in Perl. Code : $mylist[0][0]="wqeqwe"; $mylist[0][1]="afasf"; $mylist[1][0]="lkkjh"; print scalar(@mylist), "\n"; if (defined($mylist[2][0])){print "TRUE\n";} print…
384X21
  • 6,553
  • 3
  • 17
  • 17
2
votes
2 answers

Understanding the difference in behaviour between perl's assign-or operator and the combination of assignment and logical-or operators

Today I was surprised when I came across the following behaviour in perl: sub f { die if %{ $_[0] }; 42 } my %h; $h{x} ||= f(\%h); # we die. $_[0] references a hash with an 'x' key during f's run-time In contrast, given the same set-up, the…
rafl
  • 11,980
  • 2
  • 55
  • 77
2
votes
2 answers

perl check nested hash reference

I have the following code: #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $site = "test.com"; my $data = { "test" => 1 }; my $user = defined($data->{addons}->{$site}->{username}) ? $data->{addons}->{$site}->{username} :…
Zhivko Angelov
  • 139
  • 2
  • 7
2
votes
9 answers

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

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,…
Jorrit
  • 21
  • 1
  • 2
2
votes
1 answer

Why perl autovivification does not work for ->@* but ->@[0] does?

I may get values by slicing: ($x, $y, $z) = $hash->{ key }->@[0,1,2] Why I can not to write? ($x, $y, $z) = $hash->{ key }->@* For second expression in cases when key is not defined in hash I get error: Can't use an undefined value as an ARRAY…
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
2
votes
2 answers

Autovivification in PHP

if I have this SQL query: select substring(id for 2) as key, yw, count(*) from pref_money group by yw, key returning number of users per week and per key: key | yw | count -----+---------+------- VK | 2010-45 | 144 VK | 2010-44 | …
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
2
votes
2 answers

Efficiently get hash entry only if it exists in Perl

I am quite often writing fragments of code like this: if (exists $myHash->{$key}) { $value = $myHash->{$key}; } What I am trying to do is get the value from the hash if the hash has that key in it, and at the same time I want to avoid…
harmic
  • 28,606
  • 5
  • 67
  • 91