in reply to Re: parsing/munging problems
in thread parsing/munging problems

Thanks for the input guys, a few minutes after i posted this question, I thought of a new way to look at the problem. Here is my finished code. Not real pretty, but it works
sub indextime{ open FILE, "<storedata.txt"; open OUTP, ">exporteddata.txt"; select STDOUT; while (<FILE>){ push @matrix, [split]; } # print $matrix[0][0]; $storenum = $matrix[0][0]; $date = $matrix[0][1]; @matrix[0] = undef; @matrix[1] = undef; @matrix[2] = undef; @matrix[3] = undef; select OUTP; for $i (6.. ($#matrix-1)){ print "@{$matrix[$i]} $storenum $date\n"; }
This did exactly what i wanted it to do. After i got it to read the storenum, i had it read the date as well, which happened to be the second field on the first row. It then skipped the junk lines and wrote only what i needed to the new file. I'd like to thank everyone for helping me with my attempt at a question :) Nadadogg

Replies are listed 'Best First'.
Re: Re: Re: parsing/munging problems
by Hofmator (Curate) on Feb 26, 2003 at 13:18 UTC
    It's good that you have a working version now. There are some things you could improve, though:
    • use strict;!!
    • more flexibility through non-hard coded filenames
    • error checking on opening files
    • localising $_
    • eliminate select statements

    So an improved version of your sub would look like this:

    sub indextime { my ($infile, $outfile) = @_; open my $in, '<', $infile or die "Couldn't open $infile for reading: + $!"; open my $out, '>', $outfile or die "Couldn't open $outfile for writi +ng: $!"; my @matrix; local $_; push @matrix, [split] while <$in>; my ($storenum, $date) = @{$matrix[0]}[0,1]; print $out "@{$matrix[$_]} $storenum $date\n" for 6..$#matrix-1; close $in or die $!; close $out or die $!; }

    -- Hofmator

      The hard-coded filenames were only for the proof of concept so that i knew i could make it work. i am now using use:strict, and filenames pulled out of a separate array. I'm still using select statements for now because that's what I'm most comfortable with. Nadadogg
      code looks good, ill probably use it a bit next subroutine rewrite. Of course, i wont use or die because the file may not exist in some directories if the polling doesnt go through for some stores.
        Of course, i wont use or die because the file may not exist in some directories
        The 'or die' idiom is just a reminder that the open might fail, something which should always be taken into account. The appropriate action, e.g. die-ing or ignoring, depends completely on your context ...

        -- Hofmator