perl-data-structures

How does double arrow (=>) operator work in Perl?

耗尽温柔 提交于 2019-11-27 04:48:31
I know about the hash use of the => operator, like this $ cat array.pl %ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29,); print "Rikke is $ages{Rikke} years old\n"; $ perl array.pl Rikke is 29 years old $ and I thought it was just syntax to initialize hashes, but in answers to How can I qualify a variable as const/final in Perl? , => has been used like this use Readonly; Readonly my $infilename => "input_56_12.txt"; What exactly does => mean? Are there more ways in which => can be used? The => operator in perl is basically the same as comma. The only difference is that if there's an

Perl array vs list

本秂侑毒 提交于 2019-11-26 21:55:49
I have two data structures in Perl: An array: my @array2 = ( "1", "2", "3"); for $elem (@array2) { print $elem."\n"; } Giving me the following output: 1 2 3 And a list: my @array = [ "1", "2", "3"]; for $elem (@array) { print $elem."\n"; } Giving the following output: ARRAY(0x9c90818) Obviously, I'd like to iterate over the elements in both cases, but why does the second solution give me only the reference to this array? Lists in Perl are not data structures, they are positions in the source code, determined by the context around them. Lists are basically the transient structures that Perl

Why do hash keys have different order when printing?

ぃ、小莉子 提交于 2019-11-26 17:20:58
问题 I want to build several hashes using the same keys and for the keys to have the same order when I print them. So, in the example below, the keys of $hash1 and $hash2 should always have the same order, but there should be no need to keep that order when creating the hash. use Data::Dumper; my $hash1 = { keyc => 2, key1 => 1, keya => 3, keyb => 4, }; my $hash2 = { keyc => 2, key1 => 1, keya => 3, keyb => 4, }; print Dumper $hash1, $hash2; But the output is as follows: $VAR1 = { 'key1' => 1,

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

﹥>﹥吖頭↗ 提交于 2019-11-26 14:13:50
问题 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++) {

Perl array vs list

点点圈 提交于 2019-11-26 08:06:37
问题 I have two data structures in Perl: An array: my @array2 = ( \"1\", \"2\", \"3\"); for $elem (@array2) { print $elem.\"\\n\"; } Giving me the following output: 1 2 3 And a list: my @array = [ \"1\", \"2\", \"3\"]; for $elem (@array) { print $elem.\"\\n\"; } Giving the following output: ARRAY(0x9c90818) Obviously, I\'d like to iterate over the elements in both cases, but why does the second solution give me only the reference to this array? 回答1: Lists in Perl are not data structures, they are