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

You are getting the uninitialized value warning because $result_1 is undef when fed into print. The real issue is that you are never executing the line $result_1 .= $_;. There are two possibilities I can think of for why this might happen: $res->result() might not return a string as I've assumed or $res->result() is stateful and does not return the same thing on subsequent invocations. I do not have access to documentation on Opsware::NAS::Client, so I can only guess. Two tests you may consider would be running the output through a simple print to make sure you are getting what you expect (print for split /(?<=\n)/, $res->result();) and storing the value in a scalar and feeding that into Data::Dumper (print Dumper $_ = $res->result()).

I copied your Data::Dumper output and filtering loop in to the following code:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $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 !' ]; my $result_1; for (split /(?<=\n)/, $VAR1->[0]) { next unless /^\Qsnmp-server host\E/; $result_1 .= $_; } print "---Result 1---\n$result_1\n\n";

which gives me the result

---Result 1--- snmp-server host 10.234.171.38 abcd snmp-server host 10.234.171.39 abcd snmp-server host 10.234.171.40 abcd

Does this match your expectation?

Replies are listed 'Best First'.
Re^6: pulling more data from an array
by zonevbkr (Initiate) on Aug 05, 2010 at 22:33 UTC

    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.

      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].