nadadogg has asked for the wisdom of the Perl Monks concerning the following question:

Hi, this is my first post here, and I hope someone can take a swing at this. I have some files that store information in this format: StoreNum, date, gibberish. 2 blank lines ID, Timein, Timeout, zeros1, zeros2 What im trying to do is read the StoreNum, which is the first field/first row, then strip the first line completely, then tag the StoreNum to each of the data lines. I've tried this in a few different ways, but I can't seem to get it to read the lines into an array, then each field into a different array. I can read the file into an array with the fields, but without the lines into an array, i can't tell the file to make
open <FILE>, "<storedata.txt"; open <OUTP>, ">exporteddata.txt"; $StoreNum = @line[0], @field[0]; @line[0] = undef; select <OUTP>; while (<FILE>){ push @line,$StoreNum; print ; }
This isnt my exact code, but how i would like it to function

Replies are listed 'Best First'.
Re: parsing/munging problems
by Zaxo (Archbishop) on Feb 25, 2003 at 19:39 UTC

    Here's your code with corrections.

    open FILE, '< /path/to/storedata.txt' or die $!; open OUTP, '> /path/to/exporteddata.txt' or die $!; my $StoreNum = (split ' ', <FILE>, 2)[0]; while (<FILE>){ chomp; print OUTP $_, ' ', $StoreNum, $/ or die $!; } close OUTP or die $!; close FILE or die $!;

    After Compline,
    Zaxo

Re: parsing/munging problems
by jasonk (Parson) on Feb 25, 2003 at 19:38 UTC

    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);
      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

Re: parsing/munging problems
by hsmyers (Canon) on Feb 25, 2003 at 19:40 UTC
    I'd really like to help out here, but you really haven't provided quite enough information. Could I suggest you post your actual code---start with the version that allows you to "read the file into an array". Then give an example of the nature of the fields you mention. I could guess based on names, but why guess? I'd suggest also that you put the example in a __DATA__ section and use code like:
    while (<DATA>) { . . . }
    to read it in. Given that, it should be fairly easy to give you some pointers...

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
Re: parsing/munging problems
by BrowserUk (Patriarch) on Feb 25, 2003 at 20:36 UTC

    The way I read your question, I thought that you meant that your data was comma delimited rather than space delimited in which case you would need to substitute join',',<FILE> for the corresponding piece of Zaxo's code.


    ..and remember there are a lot of things monks are supposed to be but lazy is not one of them

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: parsing/munging problems
by OM_Zen (Scribe) on Feb 26, 2003 at 15:40 UTC
    Hi ,

    my $nm = 1; my $storenum; open (Conten , "a.dat"); open (OUTP ,">storenums.dat"); while (<Conten>){ chomp; (defined $nm) ? $storenum = split('',2): print OUTP "$_$storenum\n"; undef $nm; }


    This is just a way to have the storenum with the other lines of the a.dat to the storenums.dat contents