Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    flat file problems

    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.
    Code:
    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.

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Jar your memory on what?

    What is your question?

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

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Every Perl script should include these 2 lines at the beginning of the script. They will point out lots of coding mistakes.
    Code:
    use strict;
    use warnings;
    The strict pragma forces you to declare your vars before using them.

    Code:
    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.
    Code:
    my $scratch = 'scratch.dat';
    open my $SCR, '<', $scratch
      or CgiDie("<$scratch> could not be opened for reading <$!>");
    Code:
            @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 changes
    You 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.

  • Users who have thanked FishMonger for this post:

    dkenney1 (02-07-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •