For Programmers: Free Programming Magazines  


Home > Archive > Tcl > July 2005 > retrieve array element in order









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author retrieve array element in order
powah

2005-07-27, 9:04 am

My program define an array.
How to retrieve all array element in the order I define them?

array set snmp {
1 "-c public"
2c "-c public"
3 "-u powah -a MD5 -A authpassphrase -l authPriv -x DES -X
privpassphrase"
}

puts "get snmp [array get snmp]"


This is the output:
get snmp 2c {-c public} 1 {-c public} 3 {-u powah -a MD5 -A
authpassphrase -l authPriv -x DES -X privpassphrase}

How to get this output:
get snmp 1 {-c public} 2c {-c public} 3 {-u powah -a MD5 -A
authpassphrase -l authPriv -x DES -X privpassphrase}

Ingo Leschnewsky

2005-07-27, 5:06 pm

Hi,

powah schrieb:
> My program define an array.
> How to retrieve all array element in the order I define them?
>
> array set snmp {
> 1 "-c public"
> 2c "-c public"
> 3 "-u powah -a MD5 -A authpassphrase -l authPriv -x DES -X
> privpassphrase"
> }
>
> puts "get snmp [array get snmp]"
>
>
> This is the output:
> get snmp 2c {-c public} 1 {-c public} 3 {-u powah -a MD5 -A
> authpassphrase -l authPriv -x DES -X privpassphrase}
>
> How to get this output:
> get snmp 1 {-c public} 2c {-c public} 3 {-u powah -a MD5 -A
> authpassphrase -l authPriv -x DES -X privpassphrase}


you will get this output, when you keep your data in a list instead of
an array:

set snmp {
1 "-c public"
2c "-c public"
3 "-u powah -a MD5 -A authpassphrase -l authPriv -x DES -X
privpassphrase"
}

puts "get snmp $snmp"

If you also want to use the benefits of an array, then assign the list
to an array:

array set snmpArray $snmp

Since the content of an array has no defined order, it's impossible to
get the original order of the previous list by using [array get].

Regards,
Ingo
lvirden

2005-07-27, 5:06 pm

There's no way to do what you want directly from Tcl. However, you can
effectively get what you want by adding a 3rd field, that you increment
each time you put an element into the array. Then you just sort the
results that are returned by that value.

Bryan Oakley

2005-07-27, 5:06 pm

powah wrote:
> My program define an array.
> How to retrieve all array element in the order I define them?


You cannot. Array elements aren't stored in any particular order
internally. If order is necessary, store the data as a list rather than
an array. The nice thing is, you can easily convert an ordered list to
an array for the times you want random access.

set data [list 1 "-c public" 2c "-c public" 3 "-u ..."]

# process in order:
foreach {item value} $data {
...
}
puts "the data is $data"

# process like an array
array set adata $data
puts "item 2c is $adata(2c)"
SM Ryan

2005-07-27, 5:06 pm

"powah" <wong_powah@yahoo.ca> wrote:
# My program define an array.
# How to retrieve all array element in the order I define them?
#
# array set snmp {
# 1 "-c public"
# 2c "-c public"
# 3 "-u powah -a MD5 -A authpassphrase -l authPriv -x DES -X
# privpassphrase"
# }

proc arraygetorderred arrayname {
upvar 1 $arrayname array
set indices [lsort [array names array]]
set return {}
foreach index $indices {
lappend return $index $array($index)
}
return $return
}


# puts "get snmp [arraygetorderred snmp]"

get snmp 1 {-c public} 2c {-c public} 3 {-u powah -a MD5 -A authpassphrase -l authPriv -x DES -X privpassphrase}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
So basically, you just trace.
lvirden

2005-07-28, 9:10 am

Can you explain what you mean by "in the order I define them"? I
interpreted your comment to mean that you wanted things returned so
that the first item in the set was what was returned.

What would you like to get back if this was your array definition?
array set snmp {
3 "-c public"
1c "-c public"
2 "-u powah -a MD5 -A authpassphrase -l authPriv -x DES -X
privpassphrase"

}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com