Hello Anonymous Monk,

Perl is a well known scripting language that people choose to create 'Modules' that can assist you / us to reduce our lines of code to minimum. For example reading and loading all lines of a file in an array while removing the new line characters all in one line :)

One of the modules that I usually prefer to use is IO::All.

Then your task is simplified you just need to set the pattern to match (string or even multiple strings) and grep all elements that match your pattern using grep as you have done already :).

Small sample of code bellow:

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; foreach my $file (@ARGV) { if ( -e $file ) { # double check existence my @lines = io($file)->chomp->slurp; # Chomp as you slurp if (my @matches = grep( /string/, @lines ) ) { print "Found in File: '$file':\n"; print Dumper \@matches; } } } __END__ $ perl test.pl a.txt b.txt c.txt Found in File: 'a.txt': $VAR1 = [ 'Line two interested, string' ]; Found in File: 'b.txt': $VAR1 = [ 'Line one interested, string', 'Line two interested, string' ]; __DATA__ $ cat a.txt b.txt c.txt Line one not interested Line two interested, string Line one interested, string Line two interested, string Line one not interested Line two not interested Line three not interested

You need to adjust it to your preferences but it should be close to what you want to achieve.

Update: I do not want to include the solution with the subroutine. There are several ways to put all your data together. One possible way I would suggest push. Give it a try and we will help you more if you get problems. After all programming / scripting is all about trying and failing until you get it right :)

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Question on File Handler and Subroutines in Perl by thanos1983
in thread Question on File Handler and Subroutines in Perl by Anonymous Monk

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.