Thank you,
Due to your remarques, my script works properly now :
Here is the new version after modifications (I'll be glad, if you have any seggestion 
to make it better, actually the script uses big files, more than 200MB ) :

#/usr/local/bin/perl -w 
$i=0;
$k=0;
open (INDATA, "<in_file.txt") || die("Can't open in file");
open (OUTDATA, ">out_file.txt") || die("Can't open out file");

while( my $line = <INDATA> ) {
chomp $line;
my @fields = split(",", $line);
for (my $i = 0; $i <= $#fields; $i++) {
   if( $fields[$i]=~ /subject/ ) { $k=$i}
}

$line = join(":", $fields[0], $fields[2], $fields[$k], $fields[$k+1]);
print OUTDATA "$line\n";
}
close INDATA;
close OUDATA;


Sorry to bother you with my problems,

CHAFIK.



----- Original Message ----- 
From: "Wiggins d Anconia" 
Date: Tue, 29 Jun 2004 11:26:27 -0600 
To: Philipp Traeder , [EMAIL PROTECTED] 
Subject: Re: Extracting fields from a data file 

> > On Tuesday 29 June 2004 18:44, mohammed chafik wrote: 
> > > That's what i've tried (may be it is not the best approch); 
> > 
> > If you ask me, the general approach is quite ok... 
> > 
> 
> 
> 
> > 
> > > 
> > > for ($i = 0; $i < 400; $i++) { 
> > 
> > The other source for the messages is that you're trying to access 
> values in 
> > the array @fields that do not exist. 
> > In your data file, @fields holds something like 15 records, certainly 
> not 400. 
> > I'd do something like 
> > 
> > for (my $i = 0; $i <= $#fields, $i++) { 
> > 
> > which loops until $i has reached the last field of @fields 
> ($# 
> > holds the last index of an array). 
> > 
> > > if( $fields[$i]=~ /subject/ ) { $k=$i} 
> > > } 
> > > 
> 
> Good suggestions, if we are going to stick with the loop, and we only 
> care that we advance as long as we haven't found the subject then 
> throwing a 'last' in after the assignment, would provide at least some 
> speed up. If there are a significant # of lines to parse and the subject 
> may appear early enough on the line then this could be a noticeable 
> improvement. It would also make sense to adjust $i initial point forward 
> if we can be guaranteed that subject occurs after a minimum number of 
> fields on a line, which does seem indicated since we are using 0,2 
> indexes later not relative to the subject, so $i could be set at 3 
> initially. Turn my nitpicking back off now ;-)... 
> 
> http://danconia.org 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED] 
> For additional commands, e-mail: [EMAIL PROTECTED] 
> 
> 
> 
-- 
___________________________________________________________
Sign-up for Ads Free at Mail.com
http://promo.mail.com/adsfreejump.htm


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to