Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: getting syntex error in pattern search

by 2teez (Vicar)
on Aug 22, 2012 at 10:08 UTC ( #989003=note: print w/replies, xml ) Need Help??

in reply to getting syntex error in pattern search


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989003]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-02-21 11:36 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (279 votes). Check out past polls.