Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:58:24+00:00 2026-06-14T11:58:24+00:00

I am trying to populate an array dynamically rather than setting it manually by

  • 0

I am trying to populate an array dynamically rather than setting it manually by running a counter like below:

set all_list  { <my list>}
set num_pc_a_b 10

for {set i 0 ;set j 0 ; set k 0} {$j < $num_pc_a_b} {incr j; incr i ;incr k} {
    array set link_map  {[lindex $all_list $i] $j $k 0 }
}

and hoping to access the variables like below:

foreach {key value value1 value2} [array get link_map] {
    puts "key is $key"
    puts "value is $value"
    puts "value is $value1"
    puts "value is $value2"
}

But it doesn’t work . Am I doing something wrong here ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:58:25+00:00Added an answer on June 14, 2026 at 11:58 am

    It’s not immediately obvious to me what you really wanted to do, so I’ll make some guesses.

    In either case note that your usage of array set ... appears to be wrong: this command takes the name of an array and a list which it interprets as alternating keys and values, creating an entry in that array for each key mapped to the value following that key. You seem to want to just set a specific key to a specific value on each iteration — this is done by set array(key) value syntax.

    Also note that in your example the j, i and k variables seem to have the same values on each iteration. Supposedly it’s just a left-over from a real-world code, but otherwise just one variable would be enough.

    Guess one: you want to map each key to a list of values. This won’t work like in your example as in Tcl an array maps each key to one value; this value might be a list though. If my guess is correct, the way to go is:

    for {set i 0 ;set j 0 ; set k 0} {$j < $num_pc_a_b} {incr j; incr i ;incr k} {
        set link_map([lindex $all_list $i]) [list $j $k 0]
    }
    foreach {key val} [array get link_map] {
        foreach {v1 v2 v3} $val break ;# see a note below
        puts "key is $key"
        puts "value is $v1"
        puts "value is $v2"
        puts "value is $v3"
    }
    

    Guess two: judging from your usage of foreach for displaying, you might want to not use an array at all, but rather a list:

    set links [list]
    for {set i 0 ;set j 0 ; set k 0} {$j < $num_pc_a_b} {incr j; incr i ;incr k} {
        lappend links [lindex $all_list $i] $j $k 0
    }
    foreach {key v1 v2 v3} $links {
        puts "key is $key"
        puts "value is $v1"
        puts "value is $v2"
        puts "value is $v3"
    }
    

    You could as well make not a flat list but a list of alterating keys and values, with each value being a [list $j $k 0] as in the first guess example.

    Update

    Guess three. You tagged our question as multidimensional-array. I do not see any trace of multi-dimentionality in your snippets, but if you really wanted it, the usual paradigm in Tcl for this is to use compound keys. For instance, if you want to key your array by values of j, i and k these variables have on each iteration, use something like:

    for {set i 0 ;set j 0 ; set k 0} {$j < $num_pc_a_b} {incr j; incr i ;incr k} {
        set link_map($i,$j,$k) ...
    }
    

    This creates a string concatenating three values using a comma and uses the result as the key.


    The usage of foreach ... break to explode a list is a neat trick, but if you have Tcl 8.5 or later, use lassign $list v1 v2 v3 instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am dynamically trying to populate a multidimensional array and having some trouble. I
I'm trying to populate an array with a list of dates. When I go
In the below code I am trying to populate the JSON array with JSON
I am trying to populate an HTML select list using an array of customer
Hey all i am trying to populate an array like so: Dim tmpArray As
I am trying to populate an array with the text value of a json
I am trying to populate an array with a path to a file. <?php
I am trying to populate a javascript array that will be used to pass
Hi i am trying to auto populate a 2d array based on user input.
I trying to populate the dropdown list when page was loaded.But it is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.