Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I detect a blank line in Perl?
    primarykey
    data
    text
    <ul> <li><p>How do I check a line ($_ value) is a blank line in Perl? Or another good method to check it instead of using $_?</p> <p>I want to code like this</p> <p>if ($_ eq '') # Check current line is a blank line (no any characters) { $x = 0; }</p></li> </ul> <hr> <p>I updated some code with a question solution below.</p> <p>My <strong>test.txt</strong> for parsing:</p> <pre><code> constant fixup private GemAlarmFileName = &lt;A "C:\\TMP\\ALARM.LOG"&gt; vid = 0 name = "" units = "" constant fixup private GemConfigAlarms = &lt;U1 0&gt; /* my Comment */ vid = 1 name = "CONFIGALARMS" units = "" min = &lt;U1 0&gt; max = &lt;U1 2&gt; default = &lt;U1 0&gt; </code></pre> <p>My code is below.</p> <p>That's why I need to initially set $x = 0. I am not sure if it is a normal solution or not.</p> <pre><code> sub ConstantParseAndPrint { if (/^$/) // SOLUTION! { $x = 0; } if ($x == 0) { if (/^\s*(constant)\s*(fixup|\/\*fixup\*\/|)\s*(private|)\s*(\w+)\s+=\s+&lt;([a-zA-Z0-9]+)\s+(["']?)([a-zA-Z0-9.:\\]+)\6&gt;\s*(\/\*\s*(.*?)\s*\*\/|)(\r|\n|\s)/) { $name1 = $1; # Constant $name2 = $2; # Fixup $name3 = $3; # Private $name4 = $4; $name5 = $5; $name6 = $7; $name7 = $8; # start print if (!$name7 eq '') { print DEST_XML_FILE "&lt;!-- $name7--&gt;\n"; } print DEST_XML_FILE " &lt;ECID"; print DEST_XML_FILE " logicalName=\"$name4\""; print DEST_XML_FILE " valueType=\"$name5\""; print DEST_XML_FILE " value=\"$name6\""; $x = 1; } } elsif ($x == 1) { if(/\s*vid\s*=\s*(.*?)(\s|\n|\r)/) { $nID = $1; print DEST_XML_FILE " vid=\"$nID\""; $x = 2; } } elsif ($x == 2) { if(/\s*name\s*=\s*(.*?)(\s|\n|\r)/) { $nName = $1; print DEST_XML_FILE " name=$nName"; $x = 3; } } elsif ($x == 3) { if (/\s*units\s*=\s*(.*?)(\s|\n|\r)/) { $nUnits = $1; print DEST_XML_FILE " units=$nUnits"; $x = 4; } } elsif ($x == 4) { # \s+&lt;([a-zA-Z0-9]+)\s+([a-zA-Z0-9]+)&gt;\ if (/\s*min\s*=\s+&lt;([a-zA-Z0-9]+)\s+([a-zA-Z0-9]+)&gt;(\s|\n|\r)/) { #$nMinName1 = $1; $nMinName2 = $2; # Find the nMin Value #$nMinName3 = $3; #$nMinName4 = $4; print DEST_XML_FILE " min=\"$nMinName2\""; $x = 5; } else { print DEST_XML_FILE "&gt;&lt;/ECID&gt;\n"; $x = 0; # There is no line 4 and line 5 } } elsif ($x == 5) { if (/\s*max\s*=\s+&lt;([a-zA-Z0-9]+)\s+([a-zA-Z0-9]+)&gt;(\s|\n|\r)/) { #$nMaxName1 = $1; $nMaxName2 = $2; # Find the nMax Value #$nMaxName3 = $3; #$nMaxName4 = $4; print DEST_XML_FILE " max=\"$nMaxName2\""; $x = 6; } } elsif ($x == 6) { if (/\s*default\s*=\s+&lt;([a-zA-Z0-9]+)\s+([a-zA-Z0-9]+)&gt;(\s|\n|\r)/) { #$nDefault1 = $1; $nDefault2 = $2; # Find the default Value #$nDefault3 = $3; #$nDefault4 = $4; print DEST_XML_FILE " default=\"$nDefault2\"&gt;"; print DEST_XML_FILE "&lt;/ECID&gt;\n"; $x = 0; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. COWhy are you still doing all the wrong things that I, Schwern and others pointed out in your previous questions? http://stackoverflow.com/questions/1945538/how-do-i-extract-and-parse-quoted-strings-in-perl http://stackoverflow.com/questions/1919232/how-can-i-write-xml-data-to-a-file-with-perl You haven't even replaced your incorrect use of `printf` with `print`! I pity the poor sucker who has to maintain this code. I feel bad for whoever is paying for it too. Did you not understand the point of the suggestions?
      singulars
    2. COIndeed, there are so many WTFs in this code that it's difficult to wade through them all to determine the *actual* question embedded in there. Use of global variables rather than passing arguments to the function; misuse of `printf`; numbered variables rather than sensible names (or an array); huge mountainous `if` clauses that should be handled by a switch or separate functions; it goes on and on.
      singulars
    3. CO@daotoad, THANK YOU for your comment. Actureally, i never learned perl before. This is my first project(I didn't know the diffenences between the 'printf' and 'print'). I found another perl script which wrote using my style. That's why i posted again and again. I already recognized the benefit of Schwern's suggestion and your details tutorial helped me a lot actureally. I would changed my design with your guide. It is very hard to move on based on my huge and complex Text file, btw, i could realized that why i MUST change my desing to your suggestion method. . :-)
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload