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

Ok...still trying to parse my file.
This is what I have so far: I have cleaned my original input file so that it is now 1 horrific line of colon seperated values.
Now to the current problem: I need to loop through the array with differing intervals based on values encountered in specific record spots. I get through one iteration and then it gets locked into a screwball scenario where it never changes the value again and then begins harrassing me about unitialiazed values in m//. I can't attach example records due to privacy act and all that, but the code is below.

#!/usr/local/bin/perl use strict; use warnings; ## Declare variables and assign initial variables my $pia = 0; my $ndx = 0; my $num_recs = 0; my $in_file = "d:\\code\\temp files\\epropr_clean.txt"; my $out_file = "d:\\code\temp files\\epropr_parsed.txt"; my $arraylength; my @records; my @rec_array; my $record; ## Open input file and assign to @records open IN_FILE, $in_file || die "Couldn't open $in_file: $!\n"; while (<IN_FILE>) { @records = map split(':'), $_; } close IN_FILE; shift @records; $arraylength = @records; ## Check array contents my $count = 0; foreach (@records) { if ( $_ =~ /PROJ .{3}/ ) { $count++; } } print $count.$/; ## Determine number of items in record and place in @rec_array while ($pia < $arraylength) { if ($records[$pia + 1] =~ "Sup") { $num_recs = 20; } else { if ($records[$pia + 5] =~ m/1LT|2LT|CPT|MAJ|LTC|COL/) { $num_recs = 28; } else { $num_recs = 29; } } for ($ndx = 0; $ndx < $num_recs; $ndx++) { push (@rec_array, $records[$ndx] ); } foreach (@rec_array) { $record = shift(@records) . ";"; } $pia += $num_recs; print $num_recs . " - " . $pia . "\n"; }

If someone can guide me in the right direction, I would very much appreciate it. I have been staring at this for a couple of days now and am going code-crazy.

Thanks in advance, Chuck
I don't suffer from insanity...I revel in it.

Replies are listed 'Best First'.
Re: Looping thru an array with differing intervals
by dragonchild (Archbishop) on Dec 11, 2001 at 21:39 UTC
    Lemme see if I understand what you're doing.
    #!/usr/local/bin/perl use strict; use warnings; ## Declare variables and assign initial variables my $in_file = "d:\\code\\temp files\\epropr_clean.txt"; my $out_file = "d:\\code\temp files\\epropr_parsed.txt"; ## Open input file and assign to @records my @records; open IN_FILE, $in_file || die "Couldn't open $in_file: $!\n"; push @records, split(/:/, $_) while <IN_FILE>; close IN_FILE; shift @records; my $arraylength = @records; ## Check array contents my $count = 0; /PROJ .{3}/ && $count++ for @records; print $count, $/; ## Determine number of items in @record and place in @rec_array my ($record, $curr_index) = ('', 0); while ($curr_index < $arraylength) { my $num_recs = 29; if ($records[$curr_index + 1] =~ /Sup/) { $num_recs = 20; } elsif (grep { $records[$curr_index + 5] =~ /$_/ } qw(1LT 2LT CPT + MAJ LTC COL)) { $num_recs = 28; } push @rec_array, @records[0 .. $num_recs - 1]; $record = join(';', $record, splice(@records, 0, scalar(@rec_array +)); $curr_index += $num_recs; print "$num_recs - $curr_index\n"; }
    All that's happening is that you're moving over elements from @record to @rec_array, then removing them from @record and moving them, joined by ';', to $record. What purpose does this serve?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      I intend on putting the records in a file and then importing into an excel spreadsheet for my database challenged boss.

      I was having difficulty with the fact that the records are interspersed and have differing numbers of record items.

      Chuck

        So, you have a bunch of records. Some of these have 20 columns, some have 28, and some have 29. Ahhh.... this explains much.

        What you want to use is an array of arrays. Look up references in the Camel book and read for a day. :-)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Looping thru an array with differing intervals
by Fastolfe (Vicar) on Dec 12, 2001 at 04:07 UTC

    FYI,

    This does not do what you think it does:

    open IN_FILE, $in_file || die "Couldn't open $in_file: $!\n";

    || has a very high precedence. You probably want to use the lower-precedence 'or' operator here instead. Otherwise, your code above gets evaluated more like this:

    open(IN_FILE, ($in_file || die "Couldn't open $in_file: $!\n"));

    Obviously not what you want.