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

hello fellow monks, What I'm trying to do is make a script that searches a user defiened file(mostly txt and csv files) for a user defined pattern. here is my code
$file = shift; open(MAT, "<$file") or die "bad file name"; $text = <MAT>; while($text =~ /the/g) { $x += 1; } print "found $x matches\n"; close(MAT);
what I want to do is make my pattern a $var and i think i'm supposed to use rq{} to do it. I tried doing the rq{} my self but i just foobared that code. What I'm asking is how would I turn my pattern in a $var. any help or suggjestions would help a lot (I've been through the perlop already) ----------------------------------------------------------- Height : varies depending on my speed relative to the observer Weight : is a sensation caused by the gravitational wrapping of time-space Age : time is only a persistent illusion

Replies are listed 'Best First'.
Re: regex patterns
by Cabrion (Friar) on Jan 31, 2003 at 02:38 UTC
    $pattern = shift; $file = shift; open(MAT, "<$file") or die "bad file name"; $text = <MAT>; while($text =~ /\Q$pattern\E/g) { $x += 1; } print "found $x matches\n"; close(MAT);
    The \Q and \E are regex "quotes". That means characters with special meaning lose those meanings and become literal. If you want the user to be able to use regex special characters, just remove the \Q & \E.
      Thanks ----------------------------------------------------------- Height : varies depending on my speed relative to the observer Weight : is a sensation caused by the gravitational wrapping of time-space Age : time is only a persistent illusion
        Your welcome. A few well placed <p>'s in your signature would be nice ;)
Re: regex patterns
by crouchingpenguin (Priest) on Jan 31, 2003 at 03:00 UTC
    #!/usr/bin/perl use strict; use warnings; + die unless $ARGV[0]; my ($reg,$count) = (qr/$ARGV[0]/,0); open( my $fh, '<data.txt') or die; $count += scalar( my @m = m/$reg/g ) while( <$fh> ); print $count,"\n";


    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: regex patterns
by Hofmator (Curate) on Jan 31, 2003 at 10:07 UTC
    Unless $/ is undefined somewhere before in your code (some part you didn't show us), $text=<MAT> only reads in one line of the file - which is probably not what you want.

    To read in the whole file, use something like: my $text = do { local $/; <MAT> };

    -- Hofmator

      I can seem to get the  my $text = do { local $/; <MAT> }; to work. maybe you could advise me further. Here is my complete code.
      #! perl #! usr/bin/local/perl $file = shift; $pattern = shift; open(MAT, "<$file") or die "bad file name"; my $text = do ( local $/: <MAT> ); while($text =~ /\Q$pattern\E/g) { $x += 1; } print "found $x matches\n"; close(MAT);
      that crashes on me so here is my code that works but doesn't read the whole file.
      #! perl #! usr/bin/local/perl $file = shift; $pattern = shift; open(MAT, "<$file") or die "bad file name"; $text = <MAT>; while($text =~ /\Q$pattern\E/g) { $x += 1; } print "found $x matches\n"; close(MAT);
      thanks

      Height : varies depending on my speed relative to the observer Weight : is a sensation caused by the gravitational wrapping of time-space Age : time is only a persistent illusion

        well, in your complete code you have  do ( local $/: <MAT> ) while I wrote  do { local $/; <MAT> } can you spot the two subtle differences ;-)

        There are curly braces not parentheses and there is a semicolon not a colon between the two expressions.

        You say that your code crashes on you. It would be good if you supplied the error message you are getting. Crashing sounds so much like a core dump - which is never supposed to happen.

        Hth

        -- Hofmator