×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Best way of handling a certain given datastructure

Best way of handling a certain given datastructure

Best way of handling a certain given datastructure

(OP)
Hi,

Given is the following form of datastructure
set a { DATE "2019-05-12"
{ PARAMETERS { { NAME "LENGTH" VALUE "100" }
{ NAME "HEIGHT" VALUE "314" }
{ NAME "WEIGHT" VALUE "1000" }
{ NAME "COLOR" VALUE "blue" } ) )

In reality, this datastructure is much bigger. The structure is given by a certain software as a result.
What is the advantage of storeing to key-value pairs which keys are NAME and VALUE and which first value
looks like a key and its second value is the value (instead of storing "real" key-value pairs like "Weight" 1000)?
How can I effectively access this structure to retrieve - say - the COLOR "blue" from it?

Thanks in advance!

RE: Best way of handling a certain given datastructure

Maybe a data structure like this could be used to backup several parameter settings from different dates.

For example you could have at 2019-05-12 this set of parameters
LENGTH = "100"
HEIGHT = "314"
WEIGHT = "1000"
COLOR" = "blue"
and on 2019-12-12 a set of parameters like this
LENGTH = "200"
HEIGHT = "614"
WEIGHT = "2000"
COLOR = "red"

The you could store the parameters in a data structure like above and later retrieve for given date the appropriate parameter set like this:

CODE

set parameters_store \
{
 { DATE "2019-05-12" 
   PARAMETERS { 
       { NAME "LENGTH" VALUE "100" } 
       { NAME "HEIGHT" VALUE "314" } 
       { NAME "WEIGHT" VALUE "1000"} 
       { NAME "COLOR" VALUE "blue" } } } 
 { DATE "2019-12-12" 
   PARAMETERS { 
       { NAME "LENGTH" VALUE "200" } 
       { NAME "HEIGHT" VALUE "614" } 
       { NAME "WEIGHT" VALUE "2000"} 
       { NAME "COLOR" VALUE "red" } } }
}

#puts $parameters_store


#puts -nonewline "Enter date (YYYY-MM-DD): "
#flush stdout
#set date_search [gets stdin]

set date_search "2019-12-12"
puts "Searching for parameters from date: $date_search .."

set parameters_found 0

foreach parameter_set $parameters_store {

  set date_archive [dict get $parameter_set DATE]

  if {[string equal $date_archive $date_search]} {
    set parameters_found 1
    set parameters_list [dict get $parameter_set PARAMETERS]
    puts [format ".. found paramaters from %s:\n" $date_archive] 
    foreach p $parameters_list {
      set param_name [dict get $p NAME]
      set param_value [dict get $p VALUE]
      puts [format "\t%-6s = \"%s\"" $param_name $param_value]
    }
    break
  }
}

if {!$parameters_found} {
 puts "No parameters were found for $date_search."
} 

Output:

CODE

$ tclsh tuxic.tcl
Searching for parameters from date: 2019-12-12 ..
.. found paramaters from 2019-12-12:

	LENGTH = "200"
	HEIGHT = "614"
	WEIGHT = "2000"
	COLOR  = "red" 

RE: Best way of handling a certain given datastructure

(OP)
Hi mikrom,

Thank you very much for your answer and your coding! :)

I was astonished about one detail of the layout of that structure:
The usage of NAME and VALUE as keys and (for example) "LENGTH" and "100" as
their values instead of using "LENGTH as key and "100" as its value.

I think (please correct me...that's why I am asking in this forum about this! :) )
that this layout makes a direct access to a searched key-value pair via the dict
command of TCL impossible and one needs a linear search to access a certain
information.

What do you think?

RE: Best way of handling a certain given datastructure

Hi tuxic,

I thing the same as you.
For direct searching I would use a dictionary of dictionaries like you mentioned.
From this data structure we could retrieve for given date the appropriate parameter set at similar way:

CODE

set parameters_store \
{
 "2019-05-12" 
   { LENGTH "100" 
     HEIGHT "314" 
     WEIGHT "1000" 
     COLOR "blue"} 
 "2019-12-12" 
   { LENGTH "200" 
     HEIGHT "614"
     WEIGHT "2000" 
     COLOR  "red"
   }
}

#puts $parameters_store


#puts -nonewline "Enter date (YYYY-MM-DD): "
#flush stdout
#set date_search [gets stdin]

set date_search "2019-12-12"

# direct access individual parameters for given date
set param_name "COLOR"
if {[lsearch [dict keys $parameters_store] $date_search] >= 0} {
  set parameters_dict [dict get $parameters_store $date_search]
  if {[lsearch [dict keys $parameters_dict] $param_name] >= 0} {
    set param_value [dict get $parameters_dict $param_name]
    puts [format "parameter \"%s\" from date \"%s\" : \"%s\"\n" \
      $param_name $date_search $param_value]
  } else {
    puts "A parameter named \"$param_name\" was not found.\n"
  }
} else {
  puts "No parameters were found for $date_search.\n"
}

# searching all parameters for given date
puts "Searching for parameters from date: $date_search .."

set parameters_found 0

foreach date_archive [lsort [dict keys $parameters_store]] {

  if {[string equal $date_archive $date_search]} {
    set parameters_found 1
    set parameters_dict [dict get $parameters_store $date_archive]
    puts [format ".. found paramaters from %s:\n" $date_archive]
    foreach param_name [lsort [dict keys $parameters_dict]] {
      set param_value [dict get $parameters_dict $param_name]
      puts [format "\t%-6s = \"%s\"" $param_name $param_value]      
    }
    break     
  }  
}

if {!$parameters_found} {
 puts "No parameters were found for $date_search."
} 

Output:

CODE

$ tclsh tuxic2.tcl
parameter "COLOR" from date "2019-12-12" : "red"

Searching for parameters from date: 2019-12-12 ..
.. found paramaters from 2019-12-12:

        COLOR  = "red"
        HEIGHT = "614"
        LENGTH = "200"
        WEIGHT = "2000" 

RE: Best way of handling a certain given datastructure

(OP)
Hi mikrom,

I am feeling better now... :)

A big thank you for adapting your tcl examples according to the progress in the thread! :)

I initially asked for a way to efficiently access the data structure, because I didn't understand
the reason behind this special NAME "x" VALUE "y" layout thinking of something real "deep TCL", I
had simply overlooked.

But as you also think, that a real key-value layout of a real dictionary would be better for searching, I am sure,
that there is no "deep TCL" behind the NAME "x" VALUE "y" layout -- at least when it comes to retrieving values from it.

May be I get the chance to ask the author of that software (which creates that NAME/VALUE-structure initially), for the
reason for that layout...

Will see...

Thank you very much, mikrom, for your effort to help me and your help!
Cheers! :)
tuxic


RE: Best way of handling a certain given datastructure

Hi tuxic,
I have not deep tcl knowledge, but if you have the original data structure as you mentioned, you can transform it programmatically into a simpler data structure you desired.
Here is a little example:

CODE

# -- functions
proc create_parameters_for_direct_access {parameters_store} {
  set parameters_store_result {}
  foreach parameter_set $parameters_store {
    # get date
    set param_date [dict get $parameter_set DATE]
    # get list of parameters
    set parameters_list [dict get $parameter_set PARAMETERS]
    # transform parameters into dictionary
    set parameters_dict {} 
    foreach p $parameters_list {
      set param_name [dict get $p NAME]
      set param_value [dict get $p VALUE]
      # add parameter to dictionary {NAME : VALUE .. }
      dict set parameters_dict $param_name $param_value
    }
    # add to dictionary DATE: {NAME1: VALUE1 NAME2 : VALUE2 .. }
    dict set parameters_store_result $param_date $parameters_dict 
  }
  # return resulting dictionary
  return $parameters_store_result
}

proc get_param_value {parameters_store date_search param_name} {
  set result_value {} 
  if {[lsearch [dict keys $parameters_store] $date_search] >= 0} {
    set parameters_dict [dict get $parameters_store $date_search]
    if {[lsearch [dict keys $parameters_dict] $param_name] >= 0} {
      set param_value [dict get $parameters_dict $param_name]
      set result_value $param_value
    }
  }
  return $result_value
}

proc get_parameter {data_structure date name} {
  set param_val [get_param_value $data_structure $date $name]
  if {[string equal $param_val ""]} {
    puts "* ERROR: value for date=\"$date\" and name=\"$name\" not found !"
  } else {
    puts "$date: $name = \"$param_val\""
  }
}

# --- main program ---

# data structure given
set parameters_store \
{
 { DATE "2019-05-12" 
   PARAMETERS { 
       { NAME "LENGTH" VALUE "100" } 
       { NAME "HEIGHT" VALUE "314" } 
       { NAME "WEIGHT" VALUE "1000"} 
       { NAME "COLOR" VALUE "blue" } } } 
 { DATE "2019-12-12" 
   PARAMETERS { 
       { NAME "LENGTH" VALUE "200" } 
       { NAME "HEIGHT" VALUE "614" } 
       { NAME "WEIGHT" VALUE "2000"} 
       { NAME "COLOR" VALUE "red" } } }
}

puts "original data structure:\n\t$parameters_store\n"

# create new data structure 
set parameters_store_direct_access \
  [create_parameters_for_direct_access $parameters_store]

puts "new data structure:\n\t{$parameters_store_direct_access}\n"

# access values in the new dictionary
puts "Accessing individual parameters for given date:\n"
get_parameter $parameters_store_direct_access "2019-05-12" "COLOR"
get_parameter $parameters_store_direct_access "2019-05-12" "FOO"
get_parameter $parameters_store_direct_access "2019-11-11" "COLOR"
get_parameter $parameters_store_direct_access "2019-12-12" "COLOR"
get_parameter $parameters_store_direct_access "2019-05-12" "HEIGHT"
get_parameter $parameters_store_direct_access "2019-12-12" "WEIGHT" 

Output:

CODE

$ tclsh tuxic3.tcl
original data structure:

 { DATE "2019-05-12"
   PARAMETERS {
       { NAME "LENGTH" VALUE "100" }
       { NAME "HEIGHT" VALUE "314" }
       { NAME "WEIGHT" VALUE "1000"}
       { NAME "COLOR" VALUE "blue" } } }
 { DATE "2019-12-12"
   PARAMETERS {
       { NAME "LENGTH" VALUE "200" }
       { NAME "HEIGHT" VALUE "614" }
       { NAME "WEIGHT" VALUE "2000"}
       { NAME "COLOR" VALUE "red" } } }


new data structure:
        {2019-05-12 {LENGTH 100 HEIGHT 314 WEIGHT 1000 COLOR blue} 2019-12-12 {LENGTH 200 HEIGHT 614 WEIGHT 2000 COLOR red}}

Accessing individual parameters for given date:

2019-05-12: COLOR = "blue"
* ERROR: value for date="2019-05-12" and name="FOO" not found !
* ERROR: value for date="2019-11-11" and name="COLOR" not found !
2019-12-12: COLOR = "red"
2019-05-12: HEIGHT = "314"
2019-12-12: WEIGHT = "2000" 


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close