CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Writing certin sections of file to new file

 



alalleyn
Novice

Apr 28, 2002, 3:17 AM

Post #1 of 2 (1257 views)
Writing certin sections of file to new file Can't Post

A brief explanation of what I am trying to do is copy all the information between programlisting and /programlisting to a new outputfile.

I have the following script that I think should do this for me but it seems not to work and I was wondering if somebody could tell me where I am going wrong with it and hopefully point me in the right direction. I have also enclosed a copy of my test input file incase this comes in handy.

use
strict;
use diagnostics;
use CGI();
use CGI::Carp qw(fatalsToBrowser);
use Fcntl qw(:flock);

my $query = new CGI;
my $file = $query -> param(
'file');

open (TXT, $file) or die
"Can't open $file: $!";
flock(TXT, LOCK_SH);
# no one can edit the file now.
my @text = <TXT>;

push @alllines, $_ while(<TXT>);

my $codestarts =
0;
my $codeends =
0;
my @currentlist;
my @biglist;

foreach $test (@allines) {
if ($test =~ /<\/progamlisting>/) {
$codesends =
1;
}
elsif ($codeends ==
1) {
foreach $test (@currentlist){
$currentlist = ($currentlist. $test);
}
push @biglist, $currentlist;
$currentlist =();
@currentlist = @_;
$codeends =
0;
}

if ($test =~ /<progamlisting.*>/) {
$codestarts =
1;
}
elsif ($codestarts ==
1) {
push @currentlist, $test;
}

close (TXT);

open(OUT,
">Welcome.java");
foreach $line (@currentlist){
print
"$line \n";
}

close(OUT);

Attachments: litprog.xml (2.26 KB)


Jean
User


Apr 29, 2002, 11:15 PM

Post #2 of 2 (1252 views)
Re: [alalleyn] Writing certin sections of file to new file [In reply to] Can't Post

I didn't have the time to see the $file contents, and the only thing I noticed by looking at the code is that there is a $codesends variable instead of $codeends in one place (you're using strict, so you must've noticed the probem).

Just a note: You could replace push @alllines, $_ while(<TXT>); with @alllines = <TXT>;

Sorry for the lack of useful suggestions...


Jean Spector
SQA Engineer @ Exanet
jean.spector@softhome.net


There are only 10 types of people in the world -
Those who understand binary, and those who don't.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives