0

我的问题是如何从哈希中打印特定行。到目前为止的代码(感谢 Joel Berger)是:

#!/usr/bin/env perl

use strict;
use warnings;

use LWP::Simple;

my $content = get('http://temptrax.itworks.com/temp');
my %probes = $content =~ /Probe\s*(\d)\|\s*(\-?[\d\.]+)/gx;

foreach my $probe (sort keys %probes) {
  print "$probe => $probes{$probe}\n";
}

它的输出是:

1 => 74.0
2 => -99.9
3 => 74.4
4 => 68.1

如何获得要打印的特定行?例如,如果我输入数字 1,那么只有第 1 行会打印。谢谢你看看这个。

更新:经过一番阅读,我终于弄明白了

#!/usr/bin/env perl
use v5.10.1;
use strict;
use warnings;

use LWP::Simple;

my $content = get('http://temptrax.itworks.com/temp');
my %probes = $content =~ /Probe\s*(\d)\|\s*(\-?[\d\.]+)/gx;

for ($ARGV[0]) {
  when(1) {print "$probes{1}\n"; }
  when(2) {print "$probes{2}\n"; }
  when(3) {print "$probes{3}\n"; }
  when(4) {print "$probes{4}\n"; }
  default {print "error"; }
}

UPDATE2:想出了一个更简单的方法来做到这一点

#!/usr/bin/env perl
use v5.10.1;
use warnings;

use LWP::Simple;

my $content = get('http://temptrax.itworks.com/temp');
my %probes = $content =~ /Probe\s*(\d)\|\s*(\-?[\d\.]+)/gx;

$MyVar = $ARGV[0];
print $probes{$MyVar};
4

1 回答 1

3
print $probes{1};

也许?相当简单。或者:

print "$_ => ", $probes{$_} for 1,2,4;  # selected numbers
于 2012-10-25T18:11:26.870 回答