Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a program to count the repeat numbers in the file 1

Status
Not open for further replies.

tpham1002002

Technical User
Jun 20, 2012
6
0
0
US
I am learning tcl/tk and my first task is to write a program to count the repeat number in the second column of this file with content

January 4000 300 200
February 2000 100 0
March 4000 50 10
April 4000 120 300
May 1000 100 0
June 2000 0 0
July 400 1 1
August 4000 200 100
September 2000 100 200
October 0 0 0

Then the program will write to an output file with content

January 4000 300 200 1
February 2000 100 0 1
March 4000 50 10 2
April 4000 120 300 3
May 1000 100 0 0
June 2000 0 0 2
July 400 1 1 0
August 4000 200 100 4
September 2000 100 200 3
October 0 0 0 0

Notice that the last column has been added and it counts the number in second column (from the left) if they are repeat .
It is difficult for me so I would like to have your help.
Regards
Tony
 
Hi

Your explanation not covers May and October's 0, so I ignored them for now :
Code:
[b]set[/b] fil [teal][[/teal][b]open[/b] [green][i]"tpham1002002.txt"[/i][/green][teal]][/teal]

[b]while[/b] [teal]{[/teal][teal]![/teal] [teal][[/teal][b]eof[/b] [navy]$fil[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
  [b]set[/b] line [teal][[/teal][b]gets[/b] [navy]$fil[/navy][teal]][/teal]
  [b]set[/b] col2 [teal][[/teal][b]lindex[/b] [navy]$line[/navy] [purple]1[/purple][teal]][/teal]
  [b]if[/b] [teal]{[/teal][navy]$col2[/navy][teal]!=[/teal][green][i]""[/i][/green][teal]}[/teal] [teal]{[/teal]
    [b]if[/b] [teal]{[/teal][teal][[/teal][b]array[/b] [b]get[/b] count [navy]$col2[/navy][teal]]==[/teal][green][i]""[/i][/green][teal]}[/teal] [teal]{[/teal] [b]set[/b] count[teal]([/teal][navy]$col2[/navy][teal])[/teal] [purple]0[/purple] [teal]}[/teal]
    [b]incr[/b] count[teal]([/teal][navy]$col2[/navy][teal])[/teal]
    [b]lappend[/b] line [navy]$count[/navy][teal]([/teal][navy]$col2[/navy][teal])[/teal]
  [teal]}[/teal]
  [b]puts[/b] [navy]$line[/navy]
[teal]}[/teal]

[b]close[/b] [navy]$fil[/navy]

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top