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

Help needed - arrays and files 1

Status
Not open for further replies.

ee1

Programmer
May 31, 2011
25
IL
Hi,
I have a file i am reading each line:
For eample:
4 foo b
3 bar c
2 foo_bar b
Ans so on...
Each line i split into 3 ( i j k ) and define an array named data:
lappend data($k) $i $j

At last i am going through all keys of the array and write every key value into a file
So if one key has few values i want to write each of them in different line in a file.
How can i do it?

Thanks.
 
IMHO for this purpose would be suitable a hash which elements are lists of strings, i.e. for example given above:
Code:
{'b' => ['4 - foo', '2 - foo_bar']
 'c' => ['3 - bar']}
 
can you tell me how do i add values to each hash key?

thanks!
 
Here an example:
For given file
erezb84.dat
Code:
4 foo b
3 bar c
2 foo_bar b
this script
erezb84.tcl
Code:
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Reading the lines:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
[COLOR=#804040][b]set[/b][/color] file_in [[COLOR=#804040][b]open[/b][/color] [COLOR=#ff00ff]"erezb84.dat"[/color] [COLOR=#ff00ff]"r"[/color]]
[COLOR=#804040][b]while[/b][/color] {![[COLOR=#804040][b]eof[/b][/color] [COLOR=#008080]$file_in[/color]]} {
  [COLOR=#804040][b]set[/b][/color] line [[COLOR=#804040][b]string[/b][/color] trim [[COLOR=#804040][b]gets[/b][/color] [COLOR=#008080]$file_in[/color]]]
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"'[/color][COLOR=#008080]$line[/color][COLOR=#ff00ff]'"[/color]
  [COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$line[/color] == {}} {[COLOR=#804040][b]continue[/b][/color]}

  [COLOR=#804040][b]set[/b][/color] line_list [[COLOR=#804040][b]split[/b][/color] [COLOR=#008080]$line[/color] [COLOR=#ff00ff]" "[/color]]
  [COLOR=#804040][b]foreach[/b][/color] {i j k} [COLOR=#008080]$line_list[/color] {}
[COLOR=#0000ff]  #puts "(i, j, k) = ('$i', '$j', '$k')\n"[/color]

  [COLOR=#804040][b]set[/b][/color] key [COLOR=#008080]$k[/color]
  [COLOR=#804040][b]set[/b][/color] val [COLOR=#ff00ff]"[/color][COLOR=#008080]$i[/color][COLOR=#ff00ff] - [/color][COLOR=#008080]$j[/color][COLOR=#ff00ff]"[/color]

  [COLOR=#804040][b]if[/b][/color] {[[COLOR=#804040][b]info[/b][/color] exists myhash([COLOR=#008080]$key[/color])]} {
    [COLOR=#804040][b]lappend[/b][/color] myhash([COLOR=#008080]$key[/color]) [COLOR=#008080]$val[/color] 
  } [COLOR=#804040][b]else[/b][/color] {
    [COLOR=#804040][b]set[/b][/color] myhash([COLOR=#008080]$key[/color]) [[COLOR=#804040][b]list[/b][/color] [COLOR=#008080]$val[/color]] 
  }
}
[COLOR=#804040][b]close[/b][/color] [COLOR=#008080]$file_in[/color]

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]The hash of lists:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
[COLOR=#804040][b]set[/b][/color] file_out [[COLOR=#804040][b]open[/b][/color] [COLOR=#ff00ff]"erezb84_out.dat"[/color] [COLOR=#ff00ff]"w"[/color]]
[COLOR=#0000ff]# sort the hash on keys and print the [/color]
[COLOR=#0000ff]# keys and list elements[/color]
[COLOR=#804040][b]foreach[/b][/color] key [[COLOR=#804040][b]lsort[/b][/color] [[COLOR=#804040][b]array[/b][/color] names myhash]] {
[COLOR=#0000ff]  # print all hash values key => list[/color]
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff] => {[/color][COLOR=#008080]$myhash[/color][COLOR=#ff00ff]([/color][COLOR=#008080]$key[/color][COLOR=#ff00ff])}"[/color]
[COLOR=#0000ff]  # print all list elements: key => element  [/color]
  [COLOR=#804040][b]foreach[/b][/color] lst_el [COLOR=#008080]$myhash[/color]([COLOR=#008080]$key[/color]) {
    [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"  [/color][COLOR=#008080]$key[/color][COLOR=#ff00ff] => [/color][COLOR=#008080]$lst_el[/color][COLOR=#ff00ff]"[/color]    
    [COLOR=#804040][b]puts[/b][/color] [COLOR=#008080]$file_out[/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff] => [/color][COLOR=#008080]$lst_el[/color][COLOR=#ff00ff]"[/color]
  }
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
}
[COLOR=#804040][b]close[/b][/color] [COLOR=#008080]$file_out[/color]
ouputs
Code:
C:\_mikrom\Work>tclsh85 erezb84.tcl
Reading the lines:

'4 foo b'
'3 bar c'
'2 foo_bar b'
''

The hash of lists:

b => {{4 - foo} {2 - foo_bar}}
  b => 4 - foo
  b => 2 - foo_bar


c => {{3 - bar}}
  c => 3 - bar
and writes the file
erezb84_out.dat
Code:
b => 4 - foo
b => 2 - foo_bar
c => 3 - bar
 
wow man you really helped me!
 
Hi

Here on Tek-Tips we used to thank for the received help by giving stars. Please click the

* [navy]Thank mikrom
for this valuable post![/navy]


at the bottom of mikrom's post. That way you both show your gratitude and indicate this thread as helpful.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top