I have a piece of code that takes the output from a ssh command and puts it into an array. I could put it into a sting also and for this case I use both an array and a string. After I capture the output, I split the array or string on a \n and put the output of this into an array or another string. Next, run a foreach on the array or string and find the lines I need. There has to be a better way, if someone could give me some suggestions, I would appreciate it.

### start of code where I capture the command output $exp->send("show port\n"); $exp->expect(15, [ '# ', sub { my $self = shift; @port = $self->exp_before; }], ); foreach my $line (@port) { my @PORTS = split('\n', $line); foreach my $str (@PORTS){ if ($str =~ /^\d/) { $PORT_NO = (split /\s+/, $str) [0]; $exp->send("show port $PORT_NO detail\n"); $exp->expect(15, [ '# ', sub { my $self = shift; ### Here I use a string to cature the output $output_str = $self->exp_before; }], ); foreach my $string (split('\n', $output_str)){ chomp $string; if ($string =~ /Part Number/) { $model = (split /\s+/, $string) [3]; print "Node: $NODE_NAME,Port: $PORT_NO,Part: $mo +del\n"; } } } } }

In reply to array and string split by numele

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.