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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tcl help 3

Status
Not open for further replies.

daisy4u721

Programmer
Aug 24, 2009
34
US
ok..... i have a file containing a list of product names..... and im trying to write a tcl code to has to seperate the list in different categories and open it in a new file.
for example i have a file containing this:
1156xx
2278xx
3342xx
2269xx
1125xx
3345xx
and im trying to write a code to put the list in this way in a new file.
The ones The twos The threes
1156xx 2278xx 3342xx
1125xx 2269xx 3345xx

some thing like this, im totally new to tcl..... any ideas or help please.

I think i would have to use a for loop but im not sure.
 
Hi

daisy4u721 said:
im trying to write a tcl code
Show us how are you trying and where in your code are you stuck. So we can correct your errors/misconceptions.
daisy4u721 said:
I think i would have to use a for loop but im not sure.
I do not think a [tt]for[/tt] loop would help.

Feherke.
 
set fileId [open "listforprac.txt" w]

puts $fileId "The ones The twos The threes"
puts $fileId [format "%5s %5s %5s" ]

set i [open "list2.txt" r]

for {i == 11....}
set $fileId [format "%5s"]


close $fileId



This what i'm able to come up with now, I am totally new to this. Thanks for you help, I will really appreciate it if you can correct me or give me some ideas. Thanks again.
 
Hi

Hmm... So you want to output a single file ? Previously I understood that you want 3 files, one for each starting digit. Well, in this case indeed you can use [tt]for[/tt]. For example like this :
Code:
[b]set[/b] fileId [teal][[/teal][b]open[/b] [green][i]"list2.txt"[/i][/green] r[teal]][/teal]

[b]while[/b] [teal]{[/teal][teal]![[/teal][b]eof[/b] [navy]$fileId[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
  [b]set[/b] line [teal][[/teal][b]gets[/b] [navy]$fileId[/navy][teal]][/teal]
  [b]if[/b] [teal]{[/teal][navy]$line[/navy][teal]==[/teal][teal]{}}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal]
  [b]lappend[/b] linelist[teal]([[/teal][b]string[/b] range [navy]$line[/navy] [purple]0[/purple] [purple]0[/purple][teal]])[/teal] [navy]$line[/navy]
[teal]}[/teal]

[b]close[/b] [navy]$fileId[/navy]

[b]set[/b] fileId [teal][[/teal][b]open[/b] [green][i]"listforprac.txt"[/i][/green] w[teal]][/teal]

[b]set[/b] line [teal]{}[/teal]
[b]foreach[/b] one [teal][[/teal][b]lsort[/b] [teal][[/teal]array names linelist[teal]]][/teal] [teal]{[/teal]
  [b]lappend[/b] line [green][i]"The ${one}s"[/i][/green]
[teal]}[/teal]
[b]puts[/b] [navy]$fileId[/navy] [teal][[/teal][b]join[/b] [navy]$line[/navy] [green][i]"[/i][/green][lime][i]\t[/i][/lime][green][i]"[/i][/green][teal]][/teal]

[b]set[/b] item [purple]0[/purple]
[b]while[/b] [teal]{[/teal][purple]1[/purple][teal]}[/teal] [teal]{[/teal]
  [b]set[/b] line [teal]{}[/teal]
  [b]foreach[/b] one [teal][[/teal][b]lsort[/b] [teal][[/teal]array names linelist[teal]]][/teal] [teal]{[/teal]
    [b]lappend[/b] line [teal][[/teal][b]lindex[/b] [navy]$linelist[/navy][teal]([/teal][navy]$one[/navy][teal])[/teal] [navy]$item[/navy][teal]][/teal]
  [teal]}[/teal]
  [b]if[/b] [teal]{[/teal][teal][[/teal][b]join[/b] [navy]$line[/navy] [teal]{}[/teal][teal]]==[/teal][teal]{}}[/teal] [teal]{[/teal] [b]break[/b] [teal]}[/teal]
  [b]puts[/b] [navy]$fileId[/navy] [teal][[/teal][b]join[/b] [navy]$line[/navy] [green][i]"[/i][/green][lime][i]\t[/i][/lime][green][i]"[/i][/green][teal]][/teal]
  [b]incr[/b] item
[teal]}[/teal]

[b]close[/b] [navy]$fileId[/navy]
Note that my script is abit generic, because you not specified
[ul]
[li]if the file can contain empty lines ( my script drops them )[/li]
[li]if the number of starting letters is always those 3 ( my script works with any number of distinct initials )[/li]
[li]if there are the same number of lines with each starting letter ( my script will list up to the longest column length )[/li]
[li]what is the separator between the columns ( my script uses horizontal tab ( \t ) )[/li]
[/ul]


Feherke.
 
Thank you so much feherke, this really help.

I have couple of more questions.

This is actually how the categories of the numbers i have look like:

1187xx
1148xx
1200xx
1187xx
1148xx
1200xx
1100xx
1100xx

seperating them in three columns in one file

1187xx 1148xx 1200xx
1187xx 1148xx 1100xx

How do i make the for loop go by just the first 4 number not just the first number. And also how do i open the new file in an excel spreadsheet. I tried to put "listforprac.xls" but the output is all on the first row and column.

Thanks for you help!
 
daisy4u721 said:
How do i make the for loop go by just the first 4 number not just the first number. And also how do i open the new file in an excel spreadsheet. I tried to put "listforprac.xls" but the output is all on the first row and column.
You need to create CSV-file i.e. something like this
1187xx; 1148xx; 1200xx
1187xx; 1148xx; 1100xx
 
The numbers are not in the code they are in another file and it is a lot numbers, and i cant insert the semicolons manually.
 
ok Thank you. Mikrom do have any idea on how i can modify feherke's code above to let the seperate the numbers in columns according to the first four numbers, the one he has seperates them in cloumns according to the first number.
 
Hi Daisy,
To modify feherke's code you only need to do these changes:

1. change the resulting filename
from listforprac.txt to listforprac.csv, so when you dobbleclick on it it will be opened in Excel automatically

2. in the two joins change the tabelator "\t" to semicolon ";"

And don't forget to Thank feherke for this valuable post! because he has done all work for you.


 
Mikrom,

I did the changes to told me to do but all the numbers appear on the first colon, but thanks on that i will try and figure it out.


I dont think you attually got my main question. This is what i really meant.
feherke's code does this:

The ones The twos The threes
1156xx 2278xx 3342xx
1125xx 2269xx 3345xx

seperating them with the first number which is 1 2 and 3. and that was my initial question, but im trying to modify it to do this:

The first The second The third
1187xx 1148xx 1200xx
1187xx 1148xx 1100xx

seperating them with the first four numbers which are 1187, 1148, 1200 and 1100.


Thanks! and yea i have thanked feherke, he helped me so so much......


 
Hi daisy,

If you want to have the first 4 characters as a key, then change the line
lappend linelist([string range $line 0 0]) $line
to
lappend linelist([string range $line 0 3]) $line

Then for this example data

list2.txt
Code:
1187xx
1148xx
1200xx
1187xx
1148xx
1100xx
you will get this CSV-file
Code:
The 1100s;The 1148s;The 1187s;The 1200s
1100xx;1148xx;1187xx;1200xx
;1148xx;1187xx;
which looks in Excel like this
Code:
The 1100s       The 1148s       The 1187s       The 1200s
1100xx          1148xx          1187xx          1200xx
                1148xx          1187xx
You got 4 columns instead of 3 columns. Do you understand why? It's because 1100xx and 1200xx have different first 4 characters. Therefore they do not belong to the same column.

daisy4u721 said:
and yea i have thanked feherke
Here at Tek-Tips if you want thank somebody, then click at the link with
star.gif
and give him the star.
 
Mikrom,

Thank you so much you are a genius. This is exactly what i want.

One more question how do i change the title to something else from "The 1100s" "The 1148s" "The 1187s" "The 1200s".

Thank you.

How do i quote someone on this website as you quoted me:

Quote (daisy4u721):
and yea i have thanked feherke
 
How do i go from the csv-file:

"you will get this CSV-file

CODE
The 1100s;The 1148s;The 1187s;The 1200s
1100xx;1148xx;1187xx;1200xx
;1148xx;1187xx;"

to:
"which looks in Excel like this

CODE
The 1100s The 1148s The 1187s The 1200s
1100xx 1148xx 1187xx 1200xx
1148xx 1187xx"


Thanks again! and hope i am not bothering to too much.
 
Hi Mikrom,

yea i double clicked on it but the excel file showed up like this:

"The 1100s;The 1148s;The 1187s;The 1200s
1100xx;1148xx;1187xx;1200xx
;1148xx;1187xx;"

Do you know how i can change the title to something else from "The 1100s" "The 1148s" "The 1187s" "The 1200s".

Thanks Mikrom.

 
And has your file the extension CSV and not TXT?
In my Excel it opens automatically.
Then try to start Excel, go to the menu File/Open and select as type of the file CSV (Comma Separated Values).

In the above script the header line is written in
Code:
set line {}
foreach one [lsort [array names linelist]] {
  lappend line "The ${one}s"
}
puts $fileId [join $line "\t"]
so you can change it e.g. to
Code:
set line {}
set nr 1
foreach one [lsort [array names linelist]] {
  lappend line "Column $nr"
  incr nr
}
puts $fileId [join $line ";"]
and you will get
Code:
Column 1        Column 2        Column 3        Column 4
1100xx          1148xx          1187xx          1200xx
                1148xx          1187xx
 
Hi

daisy4u721, your requirement still lacks details. In case mikrom not guessed what kind of custom column titles you want ( in which case please blame yourself ), here is another try :
Code:
[highlight]array [b]set[/b] title [teal]{[/teal][/highlight]
  [highlight][green][i]"1100"[/i][/green] [green][i]"One"[/i][/green][/highlight]
  [highlight][green][i]"1148"[/i][/green] [green][i]"Other"[/i][/green][/highlight]
  [highlight][green][i]"2009"[/i][/green] [green][i]"Another"[/i][/green][/highlight]
[highlight][teal]}[/teal][/highlight]

[b]set[/b] fileId [teal][[/teal][b]open[/b] [green][i]"list2.txt"[/i][/green] r[teal]][/teal]

[b]while[/b] [teal]{[/teal][teal]![[/teal][b]eof[/b] [navy]$fileId[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
  [b]set[/b] line [teal][[/teal][b]gets[/b] [navy]$fileId[/navy][teal]][/teal]  [b]if[/b] [teal]{[/teal][navy]$line[/navy][teal]==[/teal][teal]{}}[/teal] [teal]{[/teal] [b]continue[/b] [teal]}[/teal]
  [b]lappend[/b] linelist[teal]([[/teal][b]string[/b] range [navy]$line[/navy] [purple]0[/purple] [purple]3[/purple][teal]])[/teal] [navy]$line[/navy]
[teal]}[/teal]

[b]close[/b] [navy]$fileId[/navy]

[b]set[/b] fileId [teal][[/teal][b]open[/b] [green][i]"listforprac.csv"[/i][/green] w[teal]][/teal]

[b]set[/b] line [teal]{}[/teal]
[b]foreach[/b] one [teal][[/teal][b]lsort[/b] [teal][[/teal]array names linelist[teal]]][/teal] [teal]{[/teal]
  [b]lappend[/b] line [highlight][teal][[/teal][b]expr[/b] [teal]{[/teal][teal][[/teal]array names title [teal]-[/teal]exact [navy]$one[/navy][teal]]!=[/teal][teal]{}[/teal][teal]?[/teal][navy]$title[/navy][teal]([/teal][navy]$one[/navy][teal]):[/teal][/highlight][green][i]"The ${one}s"[/i][/green][highlight][teal]}[/teal][teal]][/teal][/highlight]
[teal]}[/teal]
[b]puts[/b] [navy]$fileId[/navy] [teal][[/teal][b]join[/b] [navy]$line[/navy] [green][i]";"[/i][/green][teal]][/teal]

[b]set[/b] item [purple]0[/purple]
[b]while[/b] [teal]{[/teal][purple]1[/purple][teal]}[/teal] [teal]{[/teal]
  [b]set[/b] line [teal]{}[/teal]
  [b]foreach[/b] one [teal][[/teal][b]lsort[/b] [teal][[/teal]array names linelist[teal]]][/teal] [teal]{[/teal]
    [b]lappend[/b] line [teal][[/teal][b]lindex[/b] [navy]$linelist[/navy][teal]([/teal][navy]$one[/navy][teal])[/teal] [navy]$item[/navy][teal]][/teal]
  [teal]}[/teal]
  [b]if[/b] [teal]{[/teal][teal][[/teal][b]join[/b] [navy]$line[/navy] [teal]{}[/teal][teal]]==[/teal][teal]{}}[/teal] [teal]{[/teal] [b]break[/b] [teal]}[/teal]
  [b]puts[/b] [navy]$fileId[/navy] [teal][[/teal][b]join[/b] [navy]$line[/navy] [green][i]";"[/i][/green][teal]][/teal]
  [b]incr[/b] item
[teal]}[/teal]

[b]close[/b] [navy]$fileId[/navy]
So in the title array you set pairs of first 4 characters and column titles. Specifying title for character group which not exist in the data file, does nothing. Not specifying title for character group which exist in the data file, falls back to the title used in my previous script.

Feherke.
 
Hi

Elegant ? After daisy4u721 finally told us that her goal is loading it in Excel, the whole approach is not so elegant. Elegant would be to generate a Symbolic Link file, writing it directly, without stacking up the whole content in the memory first. Advantage : you can process input of gigabytes; disadvantage : you can not sort the columns, they will appear in the order their first values are encountered.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top