Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: finding { and }

by higle (Chaplain)
on Oct 17, 2001 at 00:51 UTC ( [id://119252]=note: print w/replies, xml ) Need Help??


in reply to finding { and }

There are two problems with your script:

One (the main problem), when you are opening the file that you wish to read from, i.e. open (FIL, ">>$FILE") || die $!;, you are using the append operator (>>) on the $FILE, instead of the read (<) operator. This will not allow you to read in the file line by line, in fact it will not allow you to read the file at all.

Two, as already pointed out, the regexes are problematic. You're using single quotes as delimiters, instead of the standard forward slash (which is allowable), but to do so you have to put m in front of the first single quote, i.e. $word =~ m'}' to let Perl know that you're going to use something other than the forward slash. Or you could just use a forward slash, $word =~ /}/. And you have to put a backslash in front of the "{", because it's a quantifying metacharacter.

Here is a rewrite of your script that should work:
#!/usr/bin/perl -w open (LOG, ">>threadcheck.log") || &Error($!); $now = localtime; print LOG "Log Created on $now \r\n"; my $lines = 0; foreach $FILE (<*.c *.C *.cpp *.CPP *.h *.H>) { open (FIL, "<$FILE") || die $!; #changed ">>" to "<" print LOG "File $FILE was opened on $now \r\n"; while (<FIL>) { $lines++; my $brackets; foreach $word ( split ) { if ($word =~ /\{/ ) #or if ($word =~ m'\{' ) { print LOG "New open bracket \r\n"; $brackets++; } if ($word =~ /}/ ) #or if ($word =~ m'}' ) { print LOG "New closed bracket \r\n"; $brackets--; } print LOG "Number of unclosed brackets in file $FILE: $brackets\ +r\n"; } print LOG "Number of lines in file $FILE: $lines\r\n"; } close FIL } close LOG

higle

Replies are listed 'Best First'.
Re: Re: finding { and }
by dudi (Initiate) on Oct 17, 2001 at 02:18 UTC
    wow, thanks a lot for your help guys!
Re: Re: finding { and }
by Hofmator (Curate) on Oct 17, 2001 at 15:21 UTC

    Some small remarks to your point 2:

    • Using the syntax $text =~ "abc" works perfectly fine, you don't need the m// operator (unless you want to give modifiers like /i). (see perlop) print "match" if "match" =~ 'a.c';
    • You don't have to escape the { in this regex, only if it might be mistaken for the meta character then you have to escape {. Even this is OK: print "match" if 'a{2,-1}b' =~ 'a{2,-1}';

    In general - as tommyw mentioned together with other important aspects - I'd use the index function for this kind of searching for a fixed string.

    -- Hofmator

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://119252]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-04-25 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found