perl-data-structures

Perl: How to turn array into nested hash keys

☆樱花仙子☆ 提交于 2019-11-29 14:51:12
I need to convert a flat list of keys into a nested hash, as follow: my $hash = {}; my @array = qw(key1 key2 lastKey Value); ToNestedHash($hash, @array); Would do this: $hash{'key1'}{'key2'}{'lastKey'} = "Value"; Axeman sub to_nested_hash { my $ref = \shift; my $h = $$ref; my $value = pop; $ref = \$$ref->{ $_ } foreach @_; $$ref = $value; return $h; } Explanation: Take the first value as a hashref Take the last value as the value to be assigned The rest are keys. Then create a SCALAR reference to the base hash. Repeatedly: Dereference the pointer to get the hash (first time) or autovivify the

Dynamically/recursively building hashes in Perl?

你。 提交于 2019-11-29 10:14:31
I'm quite new to Perl and I'm trying to build a hash recursively and getting nowhere. I tried searching for tutorials to dynamically build hashes, but all I could find were introductory articles about hashes. I would be grateful if you point me towards the right direction or suggest a nice article/tutorial. I'm trying to read from a file which has paths in the form of one/two/three four five/six/seven/eight and I want to build a hash like VAR = { one : { two : { three : "" } } four : "" five : { six : { seven : { eight : "" } } } } The script I'm using currently is : my $finalhash = {}; my

How to use an array reference to pairs of elements

99封情书 提交于 2019-11-29 08:59:41
I am considering this answer which uses a single array reference of points, where a point is a reference to a two-element array. My original code of the question (function extract-crossing ) uses two separate arrays $x and $y here which I call like this: my @x = @{ $_[0] }; my @y = @{ $_[1] }; ... return extract_crossing(\@x, \@y); The new code below based on the answer takes (x, y) and returns a single datatype, here x intercept points: use strict; use warnings; use Math::Geometry::Planar qw(SegmentLineIntersection); use Test::Exception; sub x_intercepts { my ($points) = @_; die 'Must pass at

How do I enter a multi-line comment in Perl? [duplicate]

佐手、 提交于 2019-11-28 15:37:10
Possible Duplicate: What are the common workarounds for multi-line comments in Perl? How do I add a multi-line comment to Perl source code? Nikhil Jain POD is the official way to do multi line comments in Perl, see Multi-line comments in perl code and Better ways to make multi-line comments in Perl for more detail. From faq.perl.org[ perlfaq7 ] How can I comment out a large block of Perl code? The quick-and-dirty way to comment out more than one line of Perl is to surround those lines with Pod directives. You have to put these directives at the beginning of the line and somewhere where Perl

Perl: How to turn array into nested hash keys

冷暖自知 提交于 2019-11-28 08:44:47
问题 I need to convert a flat list of keys into a nested hash, as follow: my $hash = {}; my @array = qw(key1 key2 lastKey Value); ToNestedHash($hash, @array); Would do this: $hash{'key1'}{'key2'}{'lastKey'} = "Value"; 回答1: sub to_nested_hash { my $ref = \shift; my $h = $$ref; my $value = pop; $ref = \$$ref->{ $_ } foreach @_; $$ref = $value; return $h; } Explanation: Take the first value as a hashref Take the last value as the value to be assigned The rest are keys. Then create a SCALAR reference

Dynamically/recursively building hashes in Perl?

隐身守侯 提交于 2019-11-28 03:51:17
问题 I'm quite new to Perl and I'm trying to build a hash recursively and getting nowhere. I tried searching for tutorials to dynamically build hashes, but all I could find were introductory articles about hashes. I would be grateful if you point me towards the right direction or suggest a nice article/tutorial. I'm trying to read from a file which has paths in the form of one/two/three four five/six/seven/eight and I want to build a hash like VAR = { one : { two : { three : "" } } four : "" five

How to use an array reference to pairs of elements

你离开我真会死。 提交于 2019-11-28 02:21:18
问题 I am considering this answer which uses a single array reference of points, where a point is a reference to a two-element array. My original code of the question (function extract-crossing ) uses two separate arrays $x and $y here which I call like this: my @x = @{ $_[0] }; my @y = @{ $_[1] }; ... return extract_crossing(\@x, \@y); The new code below based on the answer takes (x, y) and returns a single datatype, here x intercept points: use strict; use warnings; use Math::Geometry::Planar qw

How to iterate through Hash (of Hashes) in Perl?

谁都会走 提交于 2019-11-27 10:36:44
问题 I have Hash where values of keys are other Hashes. Example: {'key' => {'key2' => {'key3' => 'value'}}} How can I iterate through this structure? 回答1: Is this what you want? (untested) sub for_hash { my ($hash, $fn) = @_; while (my ($key, $value) = each %$hash) { if ('HASH' eq ref $value) { for_hash $value, $fn; } else { $fn->($value); } } } my $example = {'key' => {'key2' => {'key3' => 'value'}}}; for_hash $example, sub { my ($value) = @_; # Do something with $value... }; 回答2: This answer

How do I enter a multi-line comment in Perl? [duplicate]

我与影子孤独终老i 提交于 2019-11-27 09:16:53
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: What are the common workarounds for multi-line comments in Perl? How do I add a multi-line comment to Perl source code? 回答1: POD is the official way to do multi line comments in Perl, see Multi-line comments in perl code and Better ways to make multi-line comments in Perl for more detail. From faq.perl.org[ perlfaq7 ] How can I comment out a large block of Perl code? The quick-and-dirty way to comment out more

How can I maintain the order of keys I add to a Perl hash?

你。 提交于 2019-11-27 08:38:26
How can I maintain the order of actual list after counting its occurrence using a hash in the following program? For example, <DATA> are a b e a c d a c d b etc. Using hash, i counted the occurrence of each element. and what i want is: a 3 b 2 e 1 c 2 d 2 but the following program shows me otherwise. my (%count, $line, @array_1, @array_2); while ($line = <DATA>) { $count{$line}++ if ( $line =~ /\S/ ); } @array_1 = keys(%count); @array_2 = values(%count); for(my $i=0; $i<$#array_1; $i++) { print "$array_1[$i]\t $array_2[$i]"; } Hashes are not ordered, but as usual, CPAN offers a solution: Tie: