regex - use of uninitialized value $line in string ne -


I am writing a program of matching in Perl .. but the error is happening .. I saw all the previous posts This is the case, but the solution was not found ... As I am new to perl, I am not getting this error at all ..

  using the undefined value of the line $ line In line .. and in line ...  

I am attaching a Perl file here

  Use strict Ray; Use warnings; My $ line = ""; Open (OUTFILE, "& gt; output.txt") or die ("Can not open file. \ N"); If (open file (1, "match.txt") or die "can not open file. \ N") {$ line = & lt; File1 & gt; While ($ line ne "") {if (defined ($ line)) & amp; Amp; (Line = ~ m / \ sregion \ s / i)) {print OUTFILE ("$ line")}; $ Line = & lt; File1 & gt; # The problem is here (defined ($ line) & amp; ($ line = ~ / \ svth \ s /)) {print OUTFILE ("$ line")}; $ Line = & lt; File1 & gt; My problem is that this type of data contains my match.txt file.  
  Some text here is the region Satarshi satyarthi linear Saturni satrtari id -2.1741 m -2.11741 m4.3482m 2.1741m 2.1741 mth -353.9140m353.9141m-37 9.2704m 41 9.8747m 41 9.8745m Some text here  

Please solve the problem .... Thanks

You are seeing those errors that the variable $ line contains the undef . The reason for this is undef after you access the file eof and assign a value to it ( & lt; file1 & gt; ) It is described in the perldoc -f screenline :

In scalar context, reads each call and returns the next line, until the file expires, whose After the call return "Anf"

Due to this error, it is because you are not using the traditional method of reading a file. Usually, you will read a file like this:

  while ( 

It will repeat on all the lines of the file until it reaches the end of the file After that, as you know now, readline returns undef and when the loop leaves.

It also means that you do not need to check every other line, whether $ line is defined or empty, besides you can add your regexes to one And generally can extract a lot of unnecessary code:

  while (;) (if / (b :: region | vth) b / i) { Print; }}  

This is the main reason for the functionality you do, and I'm using some Pearl phrases here: Diamond operator The file names you give to the script as arguments, or if no argument is given to STDIN. If there is no argument then many built-in functions have the default use of the $ _ variable, which is print , and while Loop status

You can also keep in mind that I use the word range \ b instead of the white space \ s in regex, and also its Use | with the non-capturing bracket (?: ...) , which means that it can match those strings.

With this simple script, you can:

  perl script.pl match.txt & gt; Output.txt  

to provide your file name.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -