Help with Array Usage

D

Deepu

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.
 
J

Jürgen Exner

Deepu said:
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

Whenever you hear the word 'unique' you should automatically think
'hash'. Just store those numbers as keys of a hash.
in the first column for the
row which starts with 'test'.

Just split() the line. BTW: I think you meant the second column
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.

Better to just append the new information to the existing file, see the
append mode of open().
I guess you know how to print() to a file?

jue
 
T

Tad J McClellan

I need some help on how i can proceed for the question below.


How to proceed depends a great deal on where you are.

How far toward solving your problem have you gotten?

I have a file <test.txt>


Part of the solution to your problem will then be opening a file for reading.

Do you know how to open a file for reading?

my $fname = 'test.txt';
open my $IN, '<', $fname or die "could not open '$fname' $!";

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


Part of the solution to your problem will then be reading from an
opened filehandle.

Do you know how to read from an opened filehandle?

Next i need to get the 'unique' numbers in the first column


There are no unique numbers in the "first" column.

There are no numbers at all in the first column.

There are no unique numbers in the second column, only duplicated numbers.

This is important, as using the proper term helps you find the solution.

perldoc -q duplicate

How can I remove duplicate elements from a list or array?

for the
row which starts with 'test'.


Errr, *every* row shown starts with 'test'.

if ( /^test\D+(\d+)/ ) {
$final{$1}++;
}

Next i need to overwrite the same file <test.txt> with the same
contents as above


No you don't.

There is not much point in rewriting what is already there.

and in addition i need to add a line


Part of the solution to your problem will then be appending
to a file.

Do you know how to append to a file?

like 'testfinal
1 0 0' for the unique numbers.

open my $OUT, '>>', $fname or die "could not open '$fname' $!";
foreach my $num ( sort {$a <=> $b} keys %final ) {
print $OUT "testfinal $num 0 0 \n";
}
close $OUT;

Thanks for the help.


If you are truly thankful, then please follow the Posting Guidelines
that are posted here frequently.
 
S

siva0825

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: $!";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top