INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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.

Jobs

print output from array and foreach command

print output from array and foreach command

(OP)
Hi, i am new to tcl programming. i have one question about array. i store data from .txt files in an array call country.

array set country {$food $place}

where $food and $place is the data that i get from the .txt file. i want to display them so i do:

foreach {$food $place} [array get country] {
puts "Food : $food Where? : $place"
}

and the output is:

Food : Burger Where? : Mexico
Food : Curry Where? : India
Food : Sushi Where? : Japan

now i want to count the total elements in array country. i try puts "[array size country]" but the output is 1.

the expected output:
Food : Burger Where? : Mexico
Food : Curry Where? : India
Food : Sushi Where? : Japan
Total elements : 3

Please help, thanks in advance :)

RE: print output from array and foreach command

Hi

There is a bug, but that is in foreach : the loop control variables should be just names, without sigil. The array size works exactly as you wrote it :

CODE --> tclsh

% array set country { Burger Mexico Curry India Sushi Japan }

% foreach {food place} [array get country] { puts "Food : $food Where? : $place" }
Food : Sushi Where? : Japan
Food : Curry Where? : India
Food : Burger Where? : Mexico

% puts "[array size country]"
3

% puts $tcl_version
8.6 

So I tend to believe the problem is somewhere else.

Feherke.
feherke.ga

RE: print output from array and foreach command

Normally, is such cases, I tend to simply work around the problem and not spend too much time to the root cause. But, best practice is to investigate why the problem occurs, of course. Depends on the priority and importance of one's code I guess...

Anyway, one most simple workaround would be running a counter in the foreach loop incrementing with 1 with each loop and simply puts $counter instead of [array size country] in the end?

thacoda

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!

Resources

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