2012-10-25 32 views
0

我的問題是如何從散列打印特定行。到目前爲止的代碼(謝謝你喬爾伯傑它)是:在perl中打印來自散列的特定行

#!/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}; 
+2

你知道如何從散列中獲得特定值嗎?如果沒有,可以去閱讀一本好書或者http://perldoc.org上的文檔 – Mat

回答

3
print $probes{1}; 

也許?相當簡單。或者:

print "$_ => ", $probes{$_} for 1,2,4; # selected numbers