in reply to parsing/munging problems

I'm glad this isn't your exact code, because it isn't even perl. If you want help with code, you should really post your code along with some sample data you are trying to parse. If I read your explanation right though, you want something like this:

open(FILE, "<storedata.txt"); open(OUTP, ">exporteddata.txt"); chomp(my $storenum = <FILE>); # get the storenum while(<FILE>) { print OUTP "$storenum $_"; } close(OUTP); close(FILE);

Replies are listed 'Best First'.
Re: Re: parsing/munging problems
by nadadogg (Acolyte) on Feb 25, 2003 at 21:11 UTC
    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
      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.