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

Thanks for the info, it's definitely useful. In an effort to simplify my question, here's some background info on what I'm doing, how I'm getting the data, etc. We use an application to manage network devices. It has a series of APIs, some are useful, some not so much. This one gives us the data but it's in an array that seems really difficult to parse / pull data out of. I'm going to focus on just one example, hopefully I'll learn this and be able to apply it to the other cases. I'm working on understanding the code that was suggested, but I think it should work for either case - one result or many results. So, our one case looks like this:

$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 !' ];

What I need is the lines that starts with 'snmp-server host' and I'd need them placed in a string. Start off each instance with the word 'no ' and finish each instance with a return:

no snmp-server host 10.234.171.38 abcd\nno snmp-server host 10.234.171 +.39 abcd\nno snmp-server host 10.234.171.40 abcd\n

When I run the suggested code (I removed the result 2 section for now), I get the following:

$VAR1 = [ 'snmp-server host 208.234.171.38 JCIPPUBL no cdp advertise-v2 no cdp run !' ]; Use of uninitialized value in concatenation (.) or string at replace_e +x10.pl line 34. ---Result 1---

using this code:

my $result_1; for (split /(?<=\n)/, $res->result()) { next unless /^\Qsnmp-server host\E/; $result_1 .= $_; } print "---Result 1---\n$result_1\n\n";

Is it odd that the error (line) is the one printing the value? It's as if the variable result_1 isn't getting any data? I have a feeling that the issue is with the array, as I've had other problems in the past trying to do other things with the data. In the past I used map, I considered split or just a substitution, but I can't seem to get just the data I want - either too much or too little. thanks, Chris

Replies are listed 'Best First'.
Re^5: pulling more data from an array
by kennethk (Abbot) on Aug 05, 2010 at 20:17 UTC
    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?

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