Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

getting syntex error in pattern search

by ulaksh (Acolyte)
on Aug 22, 2012 at 07:24 UTC ( #988967=perlquestion: print w/ replies, xml ) Need Help??
ulaksh has asked for the wisdom of the Perl Monks concerning the following question:

open FILE,"config.txt" or die "cannot open file : $!"; while(<FILE>) { $ip = ($_ =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) print "$ip"; # print $_; }
output I got is
syntax error at startandendmatch.pl line 7, near ") print" Execution of startandendmatch.pl aborted due to compilation errors.

Comment on getting syntex error in pattern search
Select or Download Code
Re: getting syntex error in pattern search
by moritz (Cardinal) on Aug 22, 2012 at 07:32 UTC
Re: getting syntex error in pattern search
by SerZKO (Beadle) on Aug 22, 2012 at 09:19 UTC
    If your code looked like this
    open FILE,"config.txt" or die "cannot open file : $!"; while(<FILE>) { $ip = ($_ =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) print "$ip"; # print $_; }
    where would you put semicolon ?
      After line 3, of course. Have you tried running the code? Also, prepending some spaces to the lines in the loop would not harm anybody.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        No I haven't tried to run the code choroba. I've just tried to point ulaksh to the right direction when it comes to syntax :-) .
Re: getting syntex error in pattern search
by 2teez (Priest) on Aug 22, 2012 at 10:08 UTC
    Hi,

    Please, format your post question effectively
    Even when that is done. There are other some other best practice I think you should still look into.

    • Avoid barewords usage like FILE, FH, etc. Instead use a lexically scoped variable like $fh etc
    • use use warnings; use strict;
    • use the 3 argument open function like so:open my $fh, '<',"config.txt" or die "cannot open file : $!";
    • Close your file handles like so: close $fh or die "can't close file: $!";
    • Your construction here will only print "1", after you have corrected the syntax error
      Use this instead:
      ... while(<$fh>){ chomp; (my $ip = $_) =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/; print $ip,$/; } ...

      I think is even alot better to TEST each of your matched value using "if" like so:
      ... while (<$fh>) { chomp; my $ip=""; if (m/(?<matched_ip>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/){ $ip = $+{matched_ip}; } print $ip,$/; } ...
    Hope this helps

Re: getting syntex error in pattern search
by AnomalousMonk (Monsignor) on Aug 22, 2012 at 12:18 UTC

    Please note that
        my $m = ($s =~ m/(\d{1,3})/);
    returns the success (true or false) of the match, and
        my ($d) = $s =~ m/(\d{1,3})/;
    returns the captured substring, if any. (And the enclosing parentheses are irrelevant in the  ($s =~ m/(\d{1,3})/) expression in this case.) See perlre, perlretut and perlrequick.

    >perl -wMstrict -le "my $s = 'a987b'; my $m = ($s =~ m/(\d{1,3})/); print qq{'$m'}; my ($d) = $s =~ m/(\d{1,3})/; print qq{'$d'}; " '1' '987'

    Also, it might be better to use the  $RE{net}{IPv4} regex from Regexp::Common::net (see Regexp::Common as the general interface module) to match the IP.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988967]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-08-02 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (55 votes), past polls