Like I said, "I assume that this is unintentional". A large fraction of the traffic through SoPW is asking monks to do people's work for them
pro bono. If you want to learn Perl, this site has a large number of very smart, very talented programmers who are willing to give you a lot of patient assistance. In my short tenure, I know I've gotten a lot of very good advice and a lot of patient hand-holding from better men than me.
you've gotten rid of the parts that I need to produce the data. Was this to show how to handle the data once I get it?
I did that because I don't have all those modules installed, so I couldn't execute your script as posted. However, as you posted the result of Data::Dumper, I used that to replicate your internal state as the crux of your question was how to manipulate the data. I try and do my best to test code before I post, since posting broken code yields more confusion than assistance. I would expect the following (obviously untested) code to replicate my output with your data collection technique:
#!/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()) {
next unless /^\Qsnmp-server host\E/;
$result_1 .= $_;
}
print "---Result 1---\n$result_1\n\n";
my $result_2;
for (split /(?<=\n)/, $res->result()) {
next unless /^\Qsnmp-server\E/;
s/^((?:\S+\s+){3})\S+/${1}Bob/;
$result_2 .= $_;
}
print "---Result 2---\n$result_2\n\n";
Regarding my algorithm for obtaining the first result, it can be broken down as follows:
- split the data string at any location preceded by ((?<=...)) a new line character (\n)
- Iterate over the resulting list in a foreach loop (Foreach Loops). As there is no explicit loop variable provided, $_ is used by default. Note that for and foreach are literal synonyms.
- Test the regular expression ^\Qsnmp-server host\E. As there is no explicitly bound variable (Binding Operators), $_ is used. ^ requires that the match start at the beginning of the string. The escapes \Q and \E (Quote and Quote like Operators) are used to handle escaping on non-word characters. While they are not required in this case, I've gotten into the habit of including them whenever I copy a string into a regular expression. If the expression does not match (unless is the opposite of if), next is executed (Statement Modifiers).
- Concatenate the result onto $result_1 (Assignment Operators).
Result 2 functions similarly to result 1 with the addition of a substitution. s/^((?:\S+\s+){3})\S+/${1}Bob/; can be broken down as follows:
- Start at the beginning of the string (^)
- Capture ((...)) three copies ((?:...){3}) of one or more non-whitespace characters (\S+) followed by one or more whitespace characters (\s+) into the first buffer.
- Match one or more non-whitespace characters (\S+)
- Replace everything that matched with the contents of the first buffer (${1}) followed by Bob
I hope this is clear. See perlre or perlretut for more info on building regular expressions. For the future, try to include sample input and expected output, wrapped in code tags, so we have something test our code against. Good luck.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.