bivouac has asked for the wisdom of the Perl Monks concerning the following question:
What I want to be able to do is take those elements in a line that are "undef" and replace the undef value with the defined element from the previous line. This is what I've written so far and it prints out the @IDS to show that the replacement I used, actually worked. Next step will be pushing the values back into the original array. Thanks.1234 5 20021201 1 0 5678 0 20021202 0 0 0 0 0 10 9120 10 20021211 0 0 6543 5 20021202 0 0 0 0 0 5 0 0 0 5
#!/usr/bin/perl -w use strict; my @IDS; while (<DATA>) { my @sections = split("\t", $_); my $id = $sections[0]; push @IDS, $id; } my $length = scalar(@IDS); foreach (@IDS) { for (my $i = 0; $i < $length; $i++) { if ($IDS[$i] =~ m/^\s+/) { $IDS[$i] = $IDS[$i-1]; } } } # print scalar(@IDS); print @IDS; __END__
1234 5 20021201 1 0 5678 0 20021202 0 0 0 0 0 10 9120 10 20021211 0 0 6543 5 20021202 0 0 0 0 0 5 0 0 0 5
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Filling In "The Gaps" in an Array
by particle (Vicar) on Dec 12, 2002 at 01:23 UTC | |
by bivouac (Beadle) on Dec 12, 2002 at 05:01 UTC | |
by bivouac (Beadle) on Dec 12, 2002 at 21:55 UTC | |
by particle (Vicar) on Dec 12, 2002 at 23:40 UTC | |
|
Re: Filling In "The Gaps" in an Array
by BrowserUk (Patriarch) on Dec 11, 2002 at 23:51 UTC | |
by tall_man (Parson) on Dec 12, 2002 at 00:38 UTC | |
by Abigail-II (Bishop) on Dec 12, 2002 at 11:34 UTC | |
by BrowserUk (Patriarch) on Dec 12, 2002 at 01:30 UTC |