MD87
MD87

Reputation: 13

Printing specific line from a hash in perl

My question is how can I print a specific line from a hash. The code so far(Thank you Joel Berger for it) is:

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

The output of it is:

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

How can I get a specific line to print? Such as if I put in number 1, then only line 1 would print. Thank you for taking a look at this.

UPDATE: I was finally able to figure it out after some reading

#!/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: Figured out an even easier way to do it

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

Upvotes: 0

Views: 217

Answers (1)

TLP
TLP

Reputation: 67900

print $probes{1};

Perhaps? Rather simple. Or:

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

Upvotes: 3

Related Questions