perl-data-structures

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

假装没事ソ 提交于 2019-12-03 06:18:28
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. Daniel Li 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'; } Bill Ruppert Use Lingua::EN::Numbers::Ordinate . From the synopsis: use Lingua::EN::Numbers::Ordinate; print ordinate(4), "\n"; # prints 4th print ordinate(-342), "\n"; # prints -342nd # Example of

What are anonymous hashes in perl?

被刻印的时光 ゝ 提交于 2019-12-03 00:22:43
$hash = { 'Man' => 'Bill', 'Woman' => 'Mary, 'Dog' => 'Ben' }; What exactly do Perl's “anonymous hashes” do? 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 %hash This is useful to be able to do, if you for example want to pass a hash as an argument to a subroutine:

Parse perl array

醉酒当歌 提交于 2019-12-02 15:05:48
问题 I'm not familiar with perl and am trying to edit an irssi translation script. The result from a web request returns as: $result = { "data" => { "translations" => [ { "translatedText" => "Halloween" } ] } } How can I fetch only the translatedText portion, so that $string = 'Halloween' Thanks. 回答1: "Halloween" could be obtained as: $result->{"data"}->{"translations"}->[0]->{"translatedText"} The arrows after the first one can be omitted, so an even shorter variant would be: $result->{"data"}{

Parse perl array

雨燕双飞 提交于 2019-12-02 09:59:18
I'm not familiar with perl and am trying to edit an irssi translation script. The result from a web request returns as: $result = { "data" => { "translations" => [ { "translatedText" => "Halloween" } ] } } How can I fetch only the translatedText portion, so that $string = 'Halloween' Thanks. "Halloween" could be obtained as: $result->{"data"}->{"translations"}->[0]->{"translatedText"} The arrows after the first one can be omitted, so an even shorter variant would be: $result->{"data"}{"translations"}[0]{"translatedText"} Basically you have multiple indirections at different levels: reference

How do I create a hash of hashes in Perl?

回眸只為那壹抹淺笑 提交于 2019-12-01 16:38:11
Based on my current understanding of hashes in Perl, I would expect this code to print "hello world." It instead prints nothing. %a=(); %b=(); $b{str} = "hello"; $a{1}=%b; $b=(); $b{str} = "world"; $a{2}=%b; print "$a{1}{str} $a{2}{str}"; I assume that a hash is just like an array, so why can't I make a hash contain another? Alexandr Ciornii You should always use "use strict;" in your program. Use references and anonymous hashes. use strict;use warnings; my %a; my %b; $b{str} = "hello"; $a{1}={%b}; %b=(); $b{str} = "world"; $a{2}={%b}; print "$a{1}{str} $a{2}{str}"; {%b} creates reference to

How do I create a hash of hashes in Perl?

瘦欲@ 提交于 2019-12-01 15:30:33
问题 Based on my current understanding of hashes in Perl, I would expect this code to print "hello world." It instead prints nothing. %a=(); %b=(); $b{str} = "hello"; $a{1}=%b; $b=(); $b{str} = "world"; $a{2}=%b; print "$a{1}{str} $a{2}{str}"; I assume that a hash is just like an array, so why can't I make a hash contain another? 回答1: You should always use "use strict;" in your program. Use references and anonymous hashes. use strict;use warnings; my %a; my %b; $b{str} = "hello"; $a{1}={%b}; %b=()

Sorting a Hash of Hashes of Array of Arrays in Perl

こ雲淡風輕ζ 提交于 2019-12-01 11:39:57
I made a hash ( %locus ) to organise my data, when I print it with Data::Dumper it shows the data structure: 'locus8 >9.2668516.276570.GABA3.1.54.6586237.218516.2718570 74280 74440 locus8' => { '3 70.75 0.995018 -1.89 -' => [ [ 'window10', 'locus8', '>9.2668516.2768570.GABA3.1.54.6586237.2718516.2718570', '74280', '74400', '-', '3', '120', '55.87', '-17.41', '-11.92', '-10.60', '-1.32', '1.57', '-1.58', '0.68', '2.76', '0.995018' ], [ 'window11', 'locus8', '>9.2668516.276570.GABA3.1.54.6586237.218516.2718570', '74320', '74440', '-', '3', '120', '70.75', '-22.97', '-17.28', '-15.07', '-2.21', '

Sorting a Hash of Hashes of Array of Arrays in Perl

一笑奈何 提交于 2019-12-01 11:39:09
问题 I made a hash ( %locus ) to organise my data, when I print it with Data::Dumper it shows the data structure: 'locus8 >9.2668516.276570.GABA3.1.54.6586237.218516.2718570 74280 74440 locus8' => { '3 70.75 0.995018 -1.89 -' => [ [ 'window10', 'locus8', '>9.2668516.2768570.GABA3.1.54.6586237.2718516.2718570', '74280', '74400', '-', '3', '120', '55.87', '-17.41', '-11.92', '-10.60', '-1.32', '1.57', '-1.58', '0.68', '2.76', '0.995018' ], [ 'window11', 'locus8', '>9.2668516.276570.GABA3.1.54

Perl Merge file

别等时光非礼了梦想. 提交于 2019-12-01 10:50:59
I have 3 or multiple files I need to merge, the data looks like this.. file 1 0334.45656 0334.45678 0335.67899 file 2 0334.89765 0335.12346 0335.56789 file 3 0334.12345 0335.45678 0335.98764 Expected output in file 4, 0334.89765 0334.89765 0334.89765 0334.12345 0335.67899 0335.12346 0335.56789 0335.45678 0335.98764 So far I have tried but data in 4rth file does not come in sorted order, #!/usr/bin/perl my %hash; my $outFile = "outFile.txt"; foreach $file(@ARGV) { print "$file\n"; open (IN, "$file") || die "cannot open file $!"; open (OUT,">>$outFile") || die "cannot open file $!"; while ( <IN>

Sort by value hash of hash of hashes Perl

落花浮王杯 提交于 2019-11-30 10:51:00
I have a hash structure similar to the following: KeyA => { Key1 => { Key4 => 4 Key5 => 9 Key6 => 10 } Key2 => { Key7 => 5 Key8 => 9 } } KeyB => { Key3 => { Key9 => 6 Key10 => 3 } } I need to print out the traversal path through the hash structure and the value at the end of the traversal, such that this is ordered by value. For example, for the above hash structure I need to print: KeyB Key3 Key10 3 KeyA Key1 Key4 4 KeyA Key2 Key7 5 KeyB Key3 Key9 6 KeyA Key2 Key8 9 KeyA Key1 Key5 9 KeyA Key1 Key6 10 Currently, to solve this I am traversing the hash structure using nested foreach loops, and