perl-data-structures

How to remove keys from hash in Perl?

坚强是说给别人听的谎言 提交于 2019-12-08 05:50:29
问题 How do I remove the key from an existing map? if (exists $sampleMap{1}) { #Here I want to remove the "1" key from sampleMap } 回答1: Use delete for deleting the hash keys: if (exists $sampleMap{1}) { delete $sampleMap{1}; #Here I want remove the "1" key from sampleMap. } For more details, take a look at delete. 来源: https://stackoverflow.com/questions/18480114/how-to-remove-keys-from-hash-in-perl

Accessing JSON decoded into a HASH and an array of hash references

∥☆過路亽.° 提交于 2019-12-08 01:59:52
问题 Given the following dumper output is there a way to iterate through each hash to list only the items under each results->id record? I want to be able to say things like: print $results{1342}{'domain'}; and have the statement return testing11.com as a result. Would I have to first read through all of results array and then use $results[$counter]{id} to access the data in there ? I'm not sure how to proceed. $VAR1 = { 'end_time' => 1466017739, 'options' => { 'hour_offset' => '00', 'timezone' =>

List of paths into hash array tree in Perl

我的未来我决定 提交于 2019-12-07 16:15:31
问题 I got an array of paths C:\A C:\B\C D:\AB and I'd like to have these in a hash array tree so I can go through them in a TT2 template. What I mean is like this: @dirs = [ { name => "C:", subs => [ { name => "A", subs => [], }, { name => "B", subs => [ { name => "C", subs => [], } ], } ] }, { name => "D:", subs => [ { name => "AB", subs => [], } ], } ] I also know that I'm probably doing brainderp here so I'm open to other approaches, only requirement is turning that list of paths into

Accessing JSON decoded into a HASH and an array of hash references

孤人 提交于 2019-12-06 09:18:20
Given the following dumper output is there a way to iterate through each hash to list only the items under each results->id record? I want to be able to say things like: print $results{1342}{'domain'}; and have the statement return testing11.com as a result. Would I have to first read through all of results array and then use $results[$counter]{id} to access the data in there ? I'm not sure how to proceed. $VAR1 = { 'end_time' => 1466017739, 'options' => { 'hour_offset' => '00', 'timezone' => 'America/New_York' }, 'field_headers' => { 'priority' => 'Priority', 'status' => 'Status', 'assignee

Perl: Sorting 2D array with multiple columns based on a particular column

﹥>﹥吖頭↗ 提交于 2019-12-06 04:36:34
问题 Pseudo code: my @unsortedArray = { ["Harry", 10], ["Tim", 8], ["Joe", 3]}; my @sortedArray = ????? Final sortedArray should be sorted based on col-2 (integers), taking care of the 1-to-1 relationship with the "Name of the person" (col-1). Final result should look like: sortedArray should be { ["Joe", 3], ["Tim", 8], ["Harry", 10] }; 回答1: You can give a predicate to sort , that is: a function which is evaluated to compare elements of the list. my @unsorted = ( ["Harry", 10], ["Tim", 8], ["Joe"

List of paths into hash array tree in Perl

﹥>﹥吖頭↗ 提交于 2019-12-05 20:00:43
I got an array of paths C:\A C:\B\C D:\AB and I'd like to have these in a hash array tree so I can go through them in a TT2 template. What I mean is like this: @dirs = [ { name => "C:", subs => [ { name => "A", subs => [], }, { name => "B", subs => [ { name => "C", subs => [], } ], } ] }, { name => "D:", subs => [ { name => "AB", subs => [], } ], } ] I also know that I'm probably doing brainderp here so I'm open to other approaches, only requirement is turning that list of paths into something you can rebuild as a tree with the TT2 Template Toolkit. Also what's that structure called? I just

How do I access a value of a nested Perl hash?

人盡茶涼 提交于 2019-12-05 19:33:24
问题 I am new to Perl and I have a problem that's very simple but I cannot find the answer when consulting my Perl book. When printing the result of Dumper($request); I get the following result: $VAR1 = bless( { '_protocol' => 'HTTP/1.1', '_content' => '', '_uri' => bless( do{\(my $o = 'http://myawesomeserver.org:8081/counter/')}, 'URI::http' ), '_headers' => bless( { 'user-agent' => 'Mozilla/5.0 (X11; U; Linux i686; en; rv:1.9.0.4) Gecko/20080528 Epiphany/2.22 Firefox/3.0', 'connection' => 'keep

Convert string “a.b.c” to $hash->{a}->{b}->{c} in Perl

强颜欢笑 提交于 2019-12-05 05:28:53
I've dynamic nested hash-refs like this: my $hash = { 'a' => { 'b' => { 'c' => 'value' } } }; I want to set the value of c to 'something' by allowing the user to input "a.b.c something". Now getting the value could be done like this: my $keys = 'a.b.c'; my $v='something'; my $h = $hash; foreach my $k(split /\./, $keys) { $h = $h->{$k}; } print $h; # "value" But how would I set the value of key c to $v so that print Dumper $hash; would reflect the change? $h is not a ref at the end of the foreach loop, so changing that won't reflect the change in $hash . Any hints how to solve the knots in my

How do I retrieve an integer's ordinal suffix in Perl (like st, nd, rd, th)

可紊 提交于 2019-12-03 16:08:18
问题 I have number and need to add the suffix: 'st', 'nd', 'rd', 'th'. So for example: if the number is 42 the suffix is 'nd' , 521 is 'st' and 113 is 'th' and so on. I need to do this in perl. Any pointers. 回答1: Try this: my $ordinal; if ($foo =~ /(?<!1)1$/) { $ordinal = 'st'; } elsif ($foo =~ /(?<!1)2$/) { $ordinal = 'nd'; } elsif ($foo =~ /(?<!1)3$/) { $ordinal = 'rd'; } else { $ordinal = 'th'; } 回答2: Use Lingua::EN::Numbers::Ordinate. From the synopsis: use Lingua::EN::Numbers::Ordinate; print

What are anonymous hashes in perl?

房东的猫 提交于 2019-12-03 08:50:59
问题 $hash = { 'Man' => 'Bill', 'Woman' => 'Mary, 'Dog' => 'Ben' }; What exactly do Perl's “anonymous hashes” do? 回答1: It is a reference to a hash that can be stored in a scalar variable. It is exactly like a regular hash, except that the curly brackets {...} creates a reference to a hash. Note the usage of different parentheses in these examples: %hash = ( foo => "bar" ); # regular hash $hash = { foo => "bar" }; # reference to anonymous (unnamed) hash $href = \%hash; # reference to named hash