gmoque has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: grep throws an exeption
by ikegami (Patriarch) on Mar 27, 2009 at 22:30 UTC
    my @return = undef; # XXX Inits the array with one undef element my @return = (); # OK Empties the array. my @return; # GOOD 'my' already creates an empty array

    By the way, exception has a rather precise meaning, and that isn't one. A warning is what it is. I would also accept error.

      Thats true, sorry I was thinking in scalar context!

      Thanks!