in reply to forward variable...
You gave the answer already (except the quotes are not needed).
my $pcname; foreach my $objItem (in $colItems) { next unless $objItem->{Description} eq "..."; $pcname = $objItem->{DNSHostName}; last; }
One catch. What happens if there 0 or 2+ matching records? You could store them in an array.
my @pcnames; foreach my $objItem (in $colItems) { next unless $objItem->{Description} eq "..."; push(@pcnames, $objItem->{DNSHostName}); }
By the way, the first snippet can also be written as
use List::Util qw( first ); my $pcname = map { $_->{DNSHostName} } first { $_->{Description} eq "..." } in $colItems;
The second snippet can also be written as
my @pcnames = map { $_->{DNSHostName} } grep { $_->{Description} eq "..." } in $colItems;
If you're expecting one, and only one match, you could easily check the number of entries in @pcnames.
my @pcnames = map { $_->{DNSHostName} } grep { $_->{Description} eq "..." } in $colItems; die("Unable to find network card\n"); if @pcnames == 0; warn("Found multiple network cards. Using the first one\n") if @pcnames != 1; my $pcname = $pcnames[0];
|
|---|