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

I am attempting to return data from an array. Code is below:

my %ignorables = map { $_ => 1 } qw([notice mpmstats: rdy bsy rd wr ka + log dns cls bsy: in); open my $error_fh, '<', 'iset_error_log'; sub findLines { # Iterates over the lines in the file, putting each into $_ while (<$error_fh>) { # Only worry about the lines containing [notice if (/\[notice/) { if (/\brdy\b/){ print "\n"; } else { print ","; } my @line = grep { not defined $ignorables{$_} } split /\s+ +/; # Cleanup s/|^\[|notice|[]]//g for @line; # remove [ from [foo # Output the line @line = join(",", @line); s/,,/,/g for @line; print @line; #this is where I would like to return the arr +ay. } } } &findLines; close $error_fh;

when I print, output is as follows:

Mon,Jun,25,23:24:43,2012,999,1,0,1,0,0,0,0,Mon,Jun,25,23:24:43,2012,1, +mod_was_ap22_http.c Mon,Jun,25,23:32:44,2012,999,1,0,1,0,0,0,0,Mon,Jun,25,23:32:44,2012,1, +mod_was_ap22_http.c Mon,Jun,25,23:33:44,2012,999,1,0,1,0,0,0,0,Mon,Jun,25,23:33:44,2012,1, +mod_was_ap22_http.c Mon,Jun,25,23:45:44,2012,999,1,0,1,0,0,0,0,Mon,Jun,25,23:45:44,2012,1, +mod_was_ap22_http.c

How do I return the array outside the subroutine? Thank you.

Replies are listed 'Best First'.
Re: Return Array from Subroutine
by toolic (Bishop) on Jul 03, 2012 at 00:14 UTC
    Something like:
    my %ignorables = map { $_ => 1 } qw([notice mpmstats: rdy bsy rd wr ka + log dns cls bsy: in); open my $error_fh, '<', 'iset_error_log'; sub findLines { # Iterates over the lines in the file, putting each into $_ my @all_lines; while (<$error_fh>) { # Only worry about the lines containing [notice if (/\[notice/) { if (/\brdy\b/){ print "\n"; } else { print ","; } my @line = grep { not defined $ignorables{$_} } split /\s+ +/; # Cleanup s/|^\[|notice|[]]//g for @line; # remove [ from [foo # Output the line @line = join(",", @line); s/,,/,/g for @line; print @line; #this is where I would like to return the arr +ay. push @all_lines, @lines; } } return @all_lines; # outside of while loop } &findLines; close $error_fh;
      I don't know exactly what you're trying to do here. However, this may be more what you want:
      $line = join(',', @line); $line =~ s/,,/,/g; push @all_lines, $line;

      I get the output:

      , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,Mon,Jun,25,02:19:09,2012,999,1,0,1,0,0,0,0Mon,Jun,25,02:19:09,2012,1, +mod_was_ap22_http.cMon,Jun,25,02:21:09,2012,999,1,0,1,0,0,0,0Mon,Jun, +25,02:21:09,2012,1,mod_was_ap22_http.cMon,Jun,25,02:25:10,2012,999,1, +0,1,0,0,0,0Mon,Jun,25,02:25:10,2012,1,mod_was_ap22_http.cMon,Jun,25,0 +2:56:10,2012,999,1,0,1,0,0,0,0Mon,Jun,25,02:56:10,2012,1,mod_sm22.cpp +Mon,Jun,25,03:00:10,2012,999,1,1,0,0,0,0,0Mon,Jun,25,03:08:10,2012,99 +9,1,0,1,0,0,0,0Mon,Jun,25,03:08:10,2012,1,mod_sm22.cppMon,Jun,25,03:1 +0:10,2012,999,1,0,1,0,0,0,0Mon,Jun,25,03:10:10,2012,1,mod_was_ap22_ht +tp.cMon,Jun,25,03:24:11,2012,999,1,0,1,0,0,0,0Mon,Jun,25,03:24:11,201 +2,1,mod_was_ap22_http.cMon,
Re: Return Array from Subroutine
by cavac (Prior) on Jul 03, 2012 at 10:58 UTC

    First of all please try to follow basic guidelines in your code.

    • Check if open() actually worked.
    • Use consistent indentation to make the codeflow easier to follow (perltidy can do this for you).
    • You should use strict; use warning;
    • For better scoping, put open and close within the sub
    • Subs are generally called like findLines();
    • Use named variables instead of the implicit $_

    Here's some pseudo-code on how to return an array:

    use strict; use warnings; use Data::Dumper; sub findLines { open(my $error_fh, '<', 'iset_error_log') or die($!); my @lines; while((my $line = <$error_fh>)) { next unless($line =~ /\[notice/); skipp all lines except notic +e # yada, do something to $line push @lines, $line; } close($error_fh); return @lines; } my @found = findLines(); print Dumper(\@found);

    To be more memory efficient, you could also return a reference to the array, like this:

    # ..... close($error_fh); return \@lines; } my $found = findLines(); print Dumper($found);

    Sorry for any bad spelling, broken formatting and missing code examples. During a slight disagreement with my bicycle (which i lost), i broke my left forearm near the elbow. I'm doing the best i can here...
Re: Return Array from Subroutine
by choroba (Cardinal) on Jul 03, 2012 at 00:14 UTC
    Deleted, misread the question.