Dear Monkers,

I am playing with grep and split but I am getting some errors and I don't understand where is the problem.

Using Expect module I am getting output from a device, and then I try to organize the output nicely with this:

sub _parse_output { my $self = shift; my @output = @_; my @return = undef; foreach(@output) { next if (!defined) my @tmp = split /\r\n/; push @return, @tmp; } return \@return; }

The main idea is to put each line into each slot of the array, then on the other hand sometimes I need to grep some data but when I use grep I get an error:

Use of uninitialized value $_ in pattern match (m//) at ./random_ssh.pl line 133.

This is line 133:

$active_sessions = grep (/Session No:/, @$output);

Where @$output contains the value returned by the _parse_output routine. Do you know what's happening?

Variable $output contains the following (I used Data::dumper):

$VAR1 = [ undef, 'show system user current ', '', 'Name: root; Login Time: 03/27/2009 17:13:48; Session No: +4367', 'Name: root; Login Time: 03/27/2009 17:30:55; Session No: +4374', 'Name: root; Login Time: 03/27/2009 17:31:16; Session No: +4376', 'Name: root; Login Time: 03/27/2009 17:31:38; Session No: +4379', 'Name: root; Login Time: 03/27/2009 17:31:47; Session No: +4380', 'Name: root; Login Time: 03/27/2009 17:31:57; Session No: +4381', 'Name: root; Login Time: 03/27/2009 17:32:17; Session No: +4382', 'Name: root; Login Time: 03/27/2009 17:32:27; Session No: +4383', 'Name: root; Login Time: 03/27/2009 17:32:46; Session No: +4385', 'Name: root; Login Time: 03/27/2009 17:34:01; Session No: +4389', 'Name: root; Login Time: 03/27/2009 18:12:08; Session No: +4397', 'Name: root; Login Time: 03/27/2009 21:45:32; Session No: +4415', 'Name: root; Login Time: 03/27/2009 22:15:05; Session No: +4423*', 'Name: root; Login Time: 03/27/2009 22:15:17; Session No: +4424', ' ' ];

Any ideas are appreciated, thanks!

gmoque


In reply to grep throws an exeption by gmoque

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.