perl - Program deleting lines in my file -
i have program trying anchor beginning of lines pipe (|). however, when execute it, reduces or deletes lines in file. see, not trying replace anything, put | @ beginning of lines. here program:
$description = qr/^[0-9]+\.[0-9]+ +.*? {2,}[0-9]+.*? {2,}[0-9]+.*? {2,}[0-9]+.*? {2,}[0-9]+.*?$/; $priceline = qr/^[0-9]+.*? {2,}[0-9]+.*?$/; $priceline2 = qr/^.*? {2,}[0-9]+\.[0-9]+ {2,}[0-9]+\$/; $code = qr/^\w{9}$/; $date = qr/^as of.*?$/; $accountnumber = qr/^account number.*?$/; $name = qr/^ceotid.*?$/; while (<>) { if (s/($description)/\|$1/gi) {print stdout "1\n"; next line;}; if (s/($priceline)/\|$1/gi) {print stdout "2\n"; next line;}; if (s/($priceline2)/\|$1/gi) {print stdout "3\n"; next line;}; if (s/($cusip)/\|$1/gi) {print stdout "4\n"; next line;}; if (s/($date)/\|$1/g) {print stdout "5\n"; next line;}; if (s/($accountnumber)/\|$1/gi) {print stdout "6\n"; next line;}; if (s/($name)/\|$1/gi) {print stdout "7\n"; next line;}; print; } print "\n\ndone"
i have tried eliminate variables 1 @ time see 1 causing malfunction, still no luck. used while statement way slow , still deletes first line of file.
any great.
edit: note have undeclared variable $cusip
seem have confused $code
. without strict
, warnings
turned on, error silent , deadly, hardly discreet, insert pipe symbols on input.
i take wild guess , because trying use label have not defined: line
. label used in following fashion:
line: while (<>) { ... next line; }
but can skip , next
, refer innermost loop. when not have label, however, seems happen in tests exits loop error
label not found "next line" @ ...
this error shown if warnings
off. never should be. first 2 lines of script write should be:
use strict; use warnings;
because make life easier.
you should know can use lookahead assertions solve trying do:
if (s/(?=$description)/|/i) { ... }
you should aware this:
print stdout "1\n";
..still prints file if using inplace edit via -p
, -i
switch. if want kind of feedback, can print stderr:
print stderr "1\n"; warn "1\n"; # same thing
also, don't have use s///
operator @ all, can check line, , use string interpolation add pipe symbol:
if (/$description/) { $_ = "|$_" }
also, don't have use next
@ all: can use plain old fashioned logic:
if (/$description/) { ... } elsif (/$priceline/) { ... }
so there have it. , please, start using strict
, warnings
.
Comments
Post a Comment