Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have the following issues that I can't seem to get a clear explanation of:use strict; if($#ARGV < 0){ die "You did not specify any files to process! : $!\n"; } if($#ARGV > 2){ die "This is only going to work on the first one in the list, I th +ink. Please try again. : $!\n"; } my $onceonly=0; while(<>) { my $infname = $ARGV; my $outfname = $infname.".txt"; my $inputtxt = ""; my $outputtxt = ""; local $/=undef; open (INPUT, "$infname") or die "$infname could not be opened.: $! +\n"; while (<INPUT>){ $inputtxt .= $_; } open (OUTPUT, ">>$outfname") or die "$outfname could not be opened +.: $!\n"; while($onceonly==0) { my $bracecounter = 0; my $startbrace = '<'; my $stopbrace = '>'; for (my $i=length($inputtxt); $i>0 && length($inputtxt)>0; $i- +=1) { #going to count the number of open braces I find, #subtracting the number of close braces I find, #storing the data into outputtxt when counter==0 #looks funny because we're working backwards. my $testchar= chop ($inputtxt); if ($bracecounter == 0 && $testchar ne "<" && $testchar ne + ">"){ $outputtxt = $testchar.$outputtxt; $outputtxt =~ s/ / /g; $outputtxt =~ s/^\s\s//g; } elsif ($testchar eq $stopbrace){ $bracecounter+=1; } elsif ($testchar eq $startbrace){ $bracecounter-=1; } } print OUTPUT $outputtxt; #print $outputtxt; $onceonly+=1; } close $infname; close $outfname; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simplify parsing a file
by valdez (Monsignor) on Apr 02, 2007 at 17:36 UTC | |
by myrrdyn (Novice) on Apr 02, 2007 at 18:30 UTC | |
|
Re: Simplify parsing a file
by saintly (Scribe) on Apr 02, 2007 at 18:24 UTC | |
by myrrdyn (Novice) on Apr 02, 2007 at 18:36 UTC | |
by saintly (Scribe) on Apr 02, 2007 at 18:57 UTC | |
|
Re: Simplify parsing a file
by betterworld (Curate) on Apr 02, 2007 at 18:37 UTC | |
by myrrdyn (Novice) on Apr 02, 2007 at 18:44 UTC | |
by chromatic (Archbishop) on Apr 02, 2007 at 19:36 UTC | |
|
Re: Simplify parsing a file
by myrrdyn (Novice) on Apr 02, 2007 at 17:34 UTC | |
by myrrdyn (Novice) on Apr 02, 2007 at 19:44 UTC |