...

View Full Version : flat file problems



dkenney1
02-05-2010, 05:04 PM
Been a long time since I did perl and my drive with all my programs fried. Word to the wise, back it up and freezing a drive doesnt always work. I am at ground zero trying to read a flat file and use that data to alter another and write the changes.

open (SCR, "scratch.dat") || &CgiDie ("SCRATCH could not be opened while attempting read");
@scra =<SCR>;
close (SCR);
$reps = 1;
#sctratch.dat is 18 lines;
while ($reps < 19)
{
foreach $mine (@scra)
{
chomp($mine);
($the_line, $country_number) = split (/\|/, $mine);
open (CNT, "country.dat") || &CgiDie ("Country list could not be opened trying to position creatures");
@ymca =<CNT>;
close (CNT);
foreach $thine (@ymca)
{
chomp($thine);
($ln, $nm, $nr, $ne, $ea, $se, $so, $sw, $we, $nw, $mn, $tt ) = split (/\|/, $thine);
if ($ln == $country_number)
{
$mn = $the_line;
open (CNT, "country.dat") || &CgiDie ("Country list could not be opened trying to position creatures to write");
#need to update country dat for changes
close (CNT);
}
} # End of for each on country file

$reps++;
} # End of for each @scra - scratch
} # End of while

&printout;
}
else
{
&signin;
}
I am trying to teach myself back, but freezing the drives didnt get back my prior programs of years ago. Please jar my memory on this one.:confused:

FishMonger
02-05-2010, 05:24 PM
Jar your memory on what?

What is your question?

Are you looking for a code critique, or are you having a specific problem?

dkenney1
02-05-2010, 05:46 PM
It was in the comments on the program part, I need to write to country dat (I guess I left out with the
right syntax etc). A code critique wouldnt hurt learning either. Scratch dat has junk (line #) and the second being country number. I want to update country dat if the country number matches and enter
the line number under mn. I need to get this to write to file right.

FishMonger
02-06-2010, 08:34 PM
Every Perl script should include these 2 lines at the beginning of the script. They will point out lots of coding mistakes.

use strict;
use warnings;
The strict pragma forces you to declare your vars before using them.


open (SCR, "scratch.dat") || &CgiDie ("SCRATCH could not be opened while attempting read");When opening a filehandle, it's better to use a lexical var for the handle and the 3 arg form of open. Normally you include the reason it failed to open, which is done by including $! in the error message. In some cases you'll want to leave out $! for security reasons, but it then should be added to a log entry.

Don't use the '&' when calling a sub. The only time it is needed is when creating a reference to the sub.

my $scratch = 'scratch.dat';
open my $SCR, '<', $scratch
or CgiDie("<$scratch> could not be opened for reading <$!>");


@scra =<SCR>;
close (SCR);
$reps = 1;
#sctratch.dat is 18 lines;
while ($reps < 19)
{
foreach $mine (@scra)Why are you slurping that data into an array and then looping over that array 18 times? Use a while loop and process the data 1 time.

Why are you opening/closing "country.dat" for each and every line in 'scratch.dat'? Since you're looping over 'scratch.dat' 18 times and that file has 18 lines, that means you'll end up opening/closing "country.dat" 18 x 18 (or 324) times. Does that make sense to you? It doesn't to me.


#need to update country dat for changesYou opened it in read mode, which means you won't be able to write to it. This is one example why it's better to use the 3 arg form of open.

There are other things I can/should mention, but I'm out of time and need to work on something else.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum