FAQ 4.62 Why don't my tied hashes make the defined/exists distinction?


This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.62: Why don't my tied hashes make the defined/exists distinction

This depends on the tied hash's implementation of EXISTS(). For example,
there isn't the concept of undef with hashes that are tied to DBM*
files. It also means that exists() and defined() do the same thing with
a DBM* file, and what they end up doing is not what they do with
ordinary hashes.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
Posted On: Wednesday 7th of November 2012 01:00:52 PM Total Views:  163
View Complete with Replies

RELATED TOPICS OF Perl PROGRAMMING LANGUAGE




FAQ 4.62 What's the difference between "delete" and "undef" with hashes?

This is an excerpt from the latest version perlfaq4.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 4.62: What's the difference between "delete" and "undef" with hashes Hashes contain pairs of scalars: the first is the key, the second is the value. The key will be coerced to a string, although the value can be any kind of scalar: string, number, or reference. If a key $key is present in %hash, "exists($hash{$key})" will return true. The value for a given key can be "undef", in which case $hash{$key} will be "undef" while "exists $hash{$key}" will return true. This corresponds to ($key, "undef") being in the hash. Pictures help... here's the %hash table: keys values +------+------+ | a | 3 | | x | 7 | | d | 0 | | e | 2 | +------+------+ And these conditions hold $hash{'a'} is true $hash{'d'} is false defined $hash{'d'} is true defined $hash{'a'} is true exists $hash{'a'} is true (Perl 5 only) grep ($_ eq 'a', keys %hash) is true If you now say undef $hash{'a'} your table now reads: keys values +------+------+ | a | undef| | x | 7 | | d | 0 | | e | 2 | +------+------+ and these conditions now hold; changes in caps: $hash{'a'} is FALSE $hash{'d'} is false defined $hash{'d'} is true defined $hash{'a'} is FALSE exists $hash{'a'} is true (Perl 5 only) grep ($_ eq 'a', keys %hash) is true Notice the last two: you have an undef value, but a defined key! Now, consider this: delete $hash{'a'} your table now reads: keys values +------+------+ | x | 7 | | d | 0 | | e | 2 | +------+------+ and these conditions now hold; changes in caps: $hash{'a'} is false $hash{'d'} is false defined $hash{'d'} is true defined $hash{'a'} is false exists $hash{'a'} is FALSE (Perl 5 only) grep ($_ eq 'a', keys %hash) is FALSE See, the whole entry is gone! -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

100

Posted on:

Tuesday 16th October 2012
View Replies!

Tkx perl~tcl tied variables question

# This kind of thing works: # -textvariable => \$LABEL # But this kind of thing, in a listbox, doesn't work: # -listvariable => \@LIST # (If I had access to the Tkx $interp, # I could write maybe # tie @PerlArray, Tcl::Var, $interp, "tclList", $flags; # or something like that. ) # ------------------------------------------------------------------------ # The following (in the approved Tkx style) # shows what I mean. # (note: -justify doesn't work either.) use strict; use warnings; use Tkx qw(MainLoop); my $mw = Tkx::widget->new("."); my $LABEL = "this is a label"; my $Label = $mw->new_label(-textvariable => \$LABEL); my @LIST = ('this','is','a', 'list'); my $Listbox = $mw->new_listbox( -borderwidth =>0, -relief =>'flat', -height => 10, #-justify => 'right', -listvariable => \@LIST, ); $Listbox->insert('end','THIS'); $Listbox->insert('end','IS'); $Listbox->insert('end','A'); $Listbox->insert('end','LIST'); $Label->g_pack(); $Listbox->g_pack(); MainLoop
VIEWS ON THIS POST

118

Posted on:

Saturday 20th October 2012
View Replies!

FAQ 4.62 Why don't my tied hashes make the defined/exists distinction?

This is an excerpt from the latest version perlfaq4.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 4.62: Why don't my tied hashes make the defined/exists distinction This depends on the tied hash's implementation of EXISTS(). For example, there isn't the concept of undef with hashes that are tied to DBM* files. It also means that exists() and defined() do the same thing with a DBM* file, and what they end up doing is not what they do with ordinary hashes. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod. -- Posted via a free Usenet account from http://www.teranews.com
VIEWS ON THIS POST

233

Posted on:

Monday 22nd October 2012
View Replies!

Modules for hash functions? (ie, common algorithms for computing hash keys, not manipulating perl hashes)

Are there any modules out there for common string hashing algorithms I've got a project where I'm going to be setting up a database of texts, potentially large, amongst which we'll need to be able to identify duplicate texts quickly.
VIEWS ON THIS POST

93

Posted on:

Monday 22nd October 2012
View Replies!

FAQ 4.61 What's the difference between "delete" and "undef" with hashes?

This is an excerpt from the latest version perlfaq4.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 4.61: What's the difference between "delete" and "undef" with hashes Hashes contain pairs of scalars: the first is the key, the second is the value. The key will be coerced to a string, although the value can be any kind of scalar: string, number, or reference. If a key $key is present in %hash, "exists($hash{$key})" will return true. The value for a given key can be "undef", in which case $hash{$key} will be "undef" while "exists $hash{$key}" will return true. This corresponds to ($key, "undef") being in the hash. Pictures help... here's the %hash table: keys values +------+------+ | a | 3 | | x | 7 | | d | 0 | | e | 2 | +------+------+ And these conditions hold $hash{'a'} is true $hash{'d'} is false defined $hash{'d'} is true defined $hash{'a'} is true exists $hash{'a'} is true (Perl5 only) grep ($_ eq 'a', keys %hash) is true If you now say undef $hash{'a'} your table now reads: keys values +------+------+ | a | undef| | x | 7 | | d | 0 | | e | 2 | +------+------+ and these conditions now hold; changes in caps: $hash{'a'} is FALSE $hash{'d'} is false defined $hash{'d'} is true defined $hash{'a'} is FALSE exists $hash{'a'} is true (Perl5 only) grep ($_ eq 'a', keys %hash) is true Notice the last two: you have an undef value, but a defined key! Now, consider this: delete $hash{'a'} your table now reads: keys values +------+------+ | x | 7 | | d | 0 | | e | 2 | +------+------+ and these conditions now hold; changes in caps: $hash{'a'} is false $hash{'d'} is false defined $hash{'d'} is true defined $hash{'a'} is false exists $hash{'a'} is FALSE (Perl5 only) grep ($_ eq 'a', keys %hash) is FALSE See, the whole entry is gone! -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod. -- Posted via a free Usenet account from http://www.teranews.com
VIEWS ON THIS POST

92

Posted on:

Friday 26th October 2012
View Replies!

Perl - should I use array or hash of hashes?

A hash seems definitely the way to go, as you are collecting different data in different lines for the same\tBkupSet.However:-you don't need the first hash key\t$server\t(nor the field\tMyHost) as this is constant throughout each program run-also the field\tMyLogdate\tis constant-you can use an array for the content of the hash under the key\tBkupSet\t, as you have a fixed set of data to be collected. You would define here that, e.g., the array element [0] would be for\tMyLogdate\t(but see above), element\t[1] for\tbackup-time\t, and so on. This would be faster, but if you don't have files with millions of lines, your code is OK: it is more readable and simpler in extending to other data elements.The output with your data structure would simply be (no sorting and untested and with an additional semicolon at EOL):CODEfor $server(keys%MyItems){\tfor $BckupSet(keys%{$MyItems{$server}}){\t\tfor(keys%{$MyItems{$server}{$BckupSet}}){\t\t\tprint$_,'=>',$MyItems{$server}{$BckupSet}{$_},';';\t\t}\t\tprint"\n";\t}}\t\t\t Francohttp://www.xcalcs.com : Online engineering calculationshttp://www.megamag.it : Magnetic brakes for fun rideshttp://www.levitans.com : Air bearing pads , You really don't like to waste bytes on white space, do you Franco!\t Annihilannic. , @xhonzi - thanks for your input.As for my data. Its not random. Although for every backup set I have, I am looking for particular attributes. But on occasion for some rows, the value will not be there.What I would like to do is create as csv type datafile with this if possible
VIEWS ON THIS POST

91

Posted on:

Sunday 4th November 2012
View Replies!

sorting array of hashes by multiple hash keys

I have an array of hashes and need to sort them, I have the following code...CODE\t\t# sort\t\tmy @sorted =\tsort { $b->{'CompanyName'} $a->{'CompanyName'} } @recs; Which is fine, but I also want to include within company order, individual name order, as there are mutiple company names for the same company as there are multiple staff.How would I sort by 'CompanyName' AND 'Surname'
VIEWS ON THIS POST

73

Posted on:

Sunday 4th November 2012
View Replies!

looping over arrayref of hashes

Hi all, I'm in need of a loop, can't seem to find what I'm looking for online. I want to loop over this array ref inserting each hash value into the DB at each iteration.I've got the following data structure which is assigned to $aref: $VAR1 = [ { 'rate' => '1.98', 'name' => 'Dollars -> Sterling' }, { 'rate' => '1.5', 'name' => 'Sterling -> Euros' }, { 'rate' => '0.75', 'name' => 'Dollars -> Euros' }, { 'rate' => '2.05', 'name' => 'Sterling -> Dollars' }, { 'rate' => '0.72', 'name' => 'Euro -> Sterling' }, { 'rate' => '1.52', 'name' => 'Euro -> Dollars' }, { 'rate' => '1.98', 'name' => 'Amex Exchange Rate' } ]; Can anyone help out with this Much appreciated. G
VIEWS ON THIS POST

81

Posted on:

Sunday 4th November 2012
View Replies!

FAQ 4.44 How do I test whether two arrays or hashes are equal?

This is an excerpt from the latest version perlfaq4.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 4.44: How do I test whether two arrays or hashes are equal The following code works for single-level arrays. It uses a stringwise comparison, and does not distinguish defined versus undefined empty strings. Modify if you have other needs. $are_equal = compare_arrays(\@frogs, \@toads); sub compare_arrays { my ($first, $second) = @_; no warnings; # silence spurious -w undef complaints return 0 unless @$first == @$second; for (my $i = 0; $i < @$first; $i++) { return 0 if $first->[$i] ne $second->[$i]; } return 1; } For multilevel structures, you may wish to use an approach more like this one. It uses the CPAN module "FreezeThaw": use FreezeThaw qw(cmpStr); @a = @b = ( "this", "that", [ "more", "stuff" ] ); printf "a and b contain %s arrays\n", cmpStr(\@a, \@b) == 0 "the same" : "different"; This approach also works for comparing hashes. Here we'll demonstrate two different answers: use FreezeThaw qw(cmpStr cmpStrHard); %a = %b = ( "this" => "that", "extra" => [ "more", "stuff" ] ); $a{EXTRA} = \%b; $b{EXTRA} = \%a; printf "a and b contain %s hashes\n", cmpStr(\%a, \%b) == 0 "the same" : "different"; printf "a and b contain %s hashes\n", cmpStrHard(\%a, \%b) == 0 "the same" : "different"; The first reports that both those the hashes contain the same data, while the second reports that they do not. Which you prefer is left as an exercise to the reader. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

100

Posted on:

Sunday 4th November 2012
View Replies!

FAQ 4.62 Why don't my tied hashes make the defined/exists distinction?

This is an excerpt from the latest version perlfaq4.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 4.62: Why don't my tied hashes make the defined/exists distinction This depends on the tied hash's implementation of EXISTS(). For example, there isn't the concept of undef with hashes that are tied to DBM* files. It also means that exists() and defined() do the same thing with a DBM* file, and what they end up doing is not what they do with ordinary hashes. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

161

Posted on:

Sunday 4th November 2012
View Replies!

FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?

This is an excerpt from the latest version perlfaq4.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 4.68: How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays Usually a hash ref, perhaps like this: $record = { NAME => "Jason", EMPNO => 132, TITLE => "deputy peon", AGE => 23, SALARY => 37_000, PALS => [ "Norbert", "Rhys", "Phineas"], }; References are documented in perlref and the upcoming perlreftut. Examples of complex data structures are given in perldsc and perllol. Examples of structures and object-oriented classes are in perltoot. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

118

Posted on:

Sunday 4th November 2012
View Replies!

List of hashes

as it is only my second week of perl I would be grateful for some help with hashes. I need to build a list of multidimensional hashes one by one, sort it by one of fields and iterate through it later. 1. building a hashes sub t { return { a => 1, b => 2, } } sub r { return ( c => t(), d => t(), ) } my %record = r(); my @records; loop #setting record fields/overwriting defaults $record{fieldA}{fieldB} = value... #pushing record on list push @records, %record #preparing new record %record = r() #sorting hashes list by one of hash fields - no idea how... .... #looping through list for $rec (@records) print $rec->{fieldA}{fieldB} Big
VIEWS ON THIS POST

73

Posted on:

Sunday 4th November 2012
View Replies!

Store hashes into array???

, I want to store a hash into an array: e.g.: like this #!/usr/bin/perl -v print "Content-type: text/html\n\n"; $h{'hello'}="hello"; $h{'by'}="by"; $a[0]=$h; and if i print out the array like this print $a[0]{'hello'}." and ".$a[0]{'by'}; but it shows nothing. can somebody explain me where is a problem\t
VIEWS ON THIS POST

71

Posted on:

Sunday 4th November 2012
View Replies!

FAQ 4.64 How can I get the unique keys from two hashes?

This is an excerpt from the latest version perlfaq4.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 4.64: How can I get the unique keys from two hashes First you extract the keys from the hashes into lists, then solve the "removing duplicates" problem described above. For example: %seen = (); for $element (keys(%foo), keys(%bar)) { $seen{$element}++; } @uniq = keys %seen; Or more succinctly: @uniq = keys %{{%foo,%bar}}; Or if you really want to save space: %seen = (); while (defined ($key = each %foo)) { $seen{$key}++; } while (defined ($key = each %bar)) { $seen{$key}++; } @uniq = keys %seen; -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

193

Posted on:

Sunday 4th November 2012
View Replies!

FAQ 4.54 Why does defined() return true on empty arrays and hashes?

This is an excerpt from the latest version perlfaq4.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 4.54: Why does defined() return true on empty arrays and hashes The short story is that you should probably only use defined on scalars or functions, not on aggregates (arrays and hashes). See "defined" in perlfunc in the 5.004 release or later of Perl for more detail. -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod.
VIEWS ON THIS POST

80

Posted on:

Monday 5th November 2012
View Replies!

Counting keys in an array of hashes?

thank you all for your input - I managed to get what I wanted done. Sorry I was not very clear on the issue, but it helped to write it out. Jim
VIEWS ON THIS POST

59

Posted on:

Monday 5th November 2012
View Replies!

Storing a sorted array of hashes

CODE\t\t\t\tuse strict; \t\t\t\tuse warnings; \t\t\t\tmy @aoh =( \t\t\t\t{ \t\t\t\t\t\t3 => 15, \t\t\t\t\t\t4 => 8, \t\t\t\t\t\t5 => 9, \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\t3 => 11, \t\t\t\t\t\t4 => 25, \t\t\t\t\t\t5 => 6, \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\t3 => 5, \t\t\t\t\t\t4 => 18, \t\t\t\t\t\t5 => 5, \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\t0 => 16, \t\t\t\t\t\t1 => 11, \t\t\t\t\t\t2 => 7, \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\t0 => 21, \t\t\t\t\t\t1 => 13, \t\t\t\t\t\t2 => 31, \t\t\t\t}, \t\t\t\t{ \t\t\t\t\t\t0 => 11, \t\t\t\t\t\t1 => 14, \t\t\t\t\t\t2 => 31, \t\t\t\t}, \t\t\t\t); \t\t\t\t#declaring a new array to store the sorted hashes \t\t\t\tmy @new; \t\t\t\tprint "\n-------------expected output------------\n"; \t\t\t\tforeach my $href (@aoh) \t\t\t\t{ \t\t\t\t#i want a new array of hashes where the hashes are sorted \t\t\t\tmy %newhash; \t\t\t\tmy @sorted_keys = sort {$href->{$b} $href->{$a} || $b $a} keys %$href; \t\t\t\t\tforeach my $key (@sorted_keys) \t\t\t\t\t{ \t\t\t\t\t\t\t\tprint "$key => $href->{$key}\n"; \t\t\t\t\t\t\t$newhash{$key} = $href->{$key}; \t\t\t\t} \t\t\t\tprint "\n"; \t\t\t\tpush(@new,\%newhash); \t\t\t\t} \t\t\t\tprint "-----------output i am getting---------------\n"; \t\t\t\tforeach my $ref(@new) \t\t\t\t{ \t\t\t\tmy @skeys = skeys %$ref; \t\t\t\t\tforeach my $key (@skeys) \t\t\t\t\t{ \t\t\t\t\t\t\tprint "$key => $ref->{$key}\n" \t\t\t\t\t} \t\t\t\t\tprint "\n"; \t\t\t\t}\t\t\t\t\t\tThe output of the program :\t\t\t\t\t\t-------------expected output------------ \t\t\t\t3 => 15 \t\t\t\t5 => 9 \t\t\t\t4 => 8 \t\t\t\t\t\t\t\t4 => 25 \t\t\t\t3 => 11 \t\t\t\t5 => 6 \t\t\t\t\t\t\t\t4 => 18 \t\t\t\t5 => 5 \t\t\t\t3 => 5 \t\t\t\t\t\t\t\t0 => 16 \t\t\t\t1 => 11 \t\t\t\t2 => 7 \t\t\t\t\t\t\t\t2 => 31 \t\t\t\t0 => 21 \t\t\t\t1 => 13 \t\t\t\t\t\t\t\t2 => 31 \t\t\t\t1 => 14 \t\t\t\t0 => 11 \t\t\t\t\t\t\t\t\t\t\t\t\t-----------output i am getting---------------\t\t\t\t4 => 8\t\t\t\t3 => 15\t\t\t\t5 => 9\t\t\t\t\t\t\t\t4 => 25\t\t\t\t3 => 11\t\t\t\t5 => 6\t\t\t\t\t\t\t\t4 => 18\t\t\t\t3 => 5\t\t\t\t5 => 5\t\t\t\t\t\t\t\t1 => 11\t\t\t\t0 => 16\t\t\t\t2 => 7\t\t\t\t\t\t\t\t1 => 13\t\t\t\t0 => 21\t\t\t\t2 => 31\t\t\t\t\t\t\t\t1 => 14\t\t\t\t0 => 11\t\t\t\t2 => 31Please tell me what am i doing wrong in storing the hashes into a new array.. how do i achieve what i want..
VIEWS ON THIS POST

197

Posted on:

Wednesday 7th November 2012
View Replies!