Re: Help with Array Usage
- From: "siva0825@xxxxxxxxx" <siva0825@xxxxxxxxx>
- Date: Sun, 17 Aug 2008 04:31:27 -0700 (PDT)
On Aug 16, 5:23 pm, Deepu <pradeep...@xxxxxxxxx> wrote:
Hi All,
I need some help on how i can proceed for the question below.
I have a file <test.txt> with the contents as below:
test 1 1 0
testa 1 1 0
test 2 2 0
test 1 2 0
test 2 3 0
testb 2 2 0
testc 3 3 0
test 2 4 0
test 3 3 0
Next i need to get the 'unique' numbers in the first column for the
row which starts with 'test'.
Next i need to overwrite the same file <test.txt> with the same
contents as above and in addition i need to add a line like 'testfinal
1 0 0' for the unique numbers.
Example Output file <test.txt>:
test 1 1 0
testa 1 1 0
test 2 2 0
test 1 2 0
test 2 3 0
testb 2 2 0
testc 3 3 0
test 2 4 0
test 3 3 0
<-- New lines below-->
testfinal 1 0 0
testfinal 2 0 0
testfinal 3 0 0
Thanks for the help.
open(I, "test.txt") or die "Can't open test.txt: $!";
open(O, "> testout.txt") or die "Can't open testout.txt for writing:
$!";
my %thash;
while (<I>) {
print O;
next if (! /^test\s+/);
my @a = split;
$thash{"$a[0]final $a[1] 0 0"} = $_;
}
print O "\n\n\n";
for my $key (sort keys %thash) {
print O $key, "\n";
}
close(I); close(O);
rename("testout.txt", "test.txt") or die "Can't rename: $!";
.
- References:
- Help with Array Usage
- From: Deepu
- Help with Array Usage
- Prev by Date: Re: Tone generation question plus Windows XP and Vista information
- Next by Date: Help: Debug perl codes
- Previous by thread: Re: Help with Array Usage
- Next by thread: FAQ 4.75 How do I define methods for every class/object?
- Index(es):
Relevant Pages
|