Hello ssimone, and welcome to the Monastery!

The code you’ve shown has some major problems:

  1. First, it’s good to see you using warnings, but you should also use strict and declare each variable with my. This may look like a little extra work, but it will save you heaps of time and effort in the long run.

  2. @params=<STDIN>;

    Have a look at the output of this little script:

    15:58 >perl -MData::Dump -wE "my @params = <STDIN>; dd \@params;" 1 2 3 ^Z ["1\n", "2\n", "3\n"] 15:59 >

    Besides the fact that the user has to guess what is required, and remember to terminate with Ctrl-Z (or its equivalent), you’ll notice that each parameter has a trailing newline character. You should investigate Perl’s inbuilt chomp function (and also put the parameter input code inside a loop).

  3. %numberoftimes={};

    %numberoftimes is a hash variable, but {} is a scalar (a reference to an empty anonymous hash). To initialise a hash, use parentheses: %numberoftimes = (); — although a simple declaration (my %numberoftimes;) is all that’s needed here.

  4. while(<FH>){

    You can’t just use a filehandle (FH) without first opening it to point to a file:

    open(FH, '<', $file) or die "Cannot open file '$file' for reading, stopped";

    See perlopentut.

  5. while(<FH>){ @lines=split("\n",$file); for $line(@lines){

    There are two problems here. First, you want to split on each line, so giving the split function the name of the file makes no sense. Second, the syntax <FH> reads a line of text from the filehandle, so splitting on newlines again makes no sense. You want something like this:

    while (my $line = <FH>) { chomp $line; for my $p (@params) { ... # use $line
  6. if (index($line,$p)){

    The builtin index function returns -1 on failure, and in Perl anything other than undef, 0, '0', or the empty string, is “true,” so the if clause won’t behave as you expect it to.

As toolic advised, you really need to read (or re-read!) perlintro and master the basics. The best way is to break your program down into its smallest parts, and work on each part until it does what you want and you understand how it works.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: PERL searching through a file by Athanasius
in thread PERL searching through a file by ssimone

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.