in reply to Re^5: pulling more data from an array
in thread pulling more data from an array

Your results is what I'd like to see, my results were not that positive. While the formatting of the data (array) should be the same, the actual data can be different. For example, some may just have one line and then an '!" as:

snmp-server host 10.10.21.14 publ !

but others may end up with more data as:

snmp-server host 10.234.171.38 abcd snmp-server host 10.234.171.39 abcd snmp-server host 10.234.171.40 abcd !snmp-server host 10.10.10.1 xyz

But they all should be formatted the same. I commented out the result_1 code and added the two print lines and got unexpected results. For

print for split /(?<=\n)/, $res->result(); I got ARRAY(0x100dd10)

Yet when I did the

print Dumper $_ = $res->result();

I got:

$VAR1 = [ 'snmp-server host 10.234.171.38 abcd snmp-server host 10.234.171.39 abcd snmp-server host 10.234.171.40 abcd !snmp-server host 10.10.10.1 xyz !' ];

I noticed this result is the same as

print (Dumper([$res->result()]));

so it's something with the array and that's causing the code not to work as planned.

Replies are listed 'Best First'.
Re^7: pulling more data from an array
by kennethk (Abbot) on Aug 05, 2010 at 22:39 UTC
    What's happening is $res->result() is returning an array reference, where that array contains a single entry. This means you need to dereference the result before feeding it into your loop. Try:

    #!/usr/bin/perl use lib qw(blib lib); use strict; use warnings; use Getopt::Long; use Pod::Usage; use Data::Dumper; use Opsware::NAS::Client; use Net::Ping; use POSIX qw(strftime); #use TrueControlAPI; my($user,$pass,$host) = ('na_admin', 'na4adm1n', 'localhost'); my $help = 0; my $nas = Opsware::NAS::Client->new(); my $res = $nas->login(-user => $user, -pass => $pass, -host => $host, +-nosession => '0'); print " \n"; #$res = $nas->show_configlet(host => "dev1", start => "snmp-server hos +t", end => "!"); $res = $nas->show_configlet(host => "dev2", start => "snmp-server host +", end => "!"); #$res = $nas->show_deviceinfo(ip => '10.253.74.70'); unless ($res->ok()) { printf STDERR ("*** error: %s\n", $res->error_message()); exit(1); } print (Dumper([$res->result()]), "\n\n"); my $result_1; for (split /(?<=\n)/, $res->result()->[0]) { next unless /^\Qsnmp-server host\E/; $result_1 .= $_; } print "---Result 1---\n$result_1\n\n"; my $result_2; for (split /(?<=\n)/, $res->result()->[0]) { next unless /^\Qsnmp-server\E/; s/^((?:\S+\s+){3})\S+/${1}Bob/; $result_2 .= $_; } print "---Result 2---\n$result_2\n\n";

    The change to note is swapping $res->result() to $res->result()->[0].