How to print multiple arrays in TCL? - arrays

#!/usr/bin/expect -f
set myarr1(chicken) animal
set myarr1(cows) animal
set myarr1(tiger) animal
set myarr1(horse) animal
set myarr2(carrot) vegetable
set myarr2(tomato) vegetable
set myarr2(potato) vegetable
set myarr2(pea) vegetable
set arr_list { myarr1 myarr2 }
foreach key [array names [lindex $arr_list 0]] {
puts "${key}=$[lindex $arr_list 0]($key)"
}
foreach key [array names [lindex $arr_list 1]] {
puts "${key}=$[lindex $arr_list 1]($key)"
}
Output obtained:
cows=$myarr1(cows)
horse=$myarr1(horse)
chicken=$myarr1(chicken)
tiger=$myarr1(tiger)
tomato=$myarr2(tomato)
pea=$myarr2(pea)
potato=$myarr2(potato)
carrot=$myarr2(carrot)
Required output:
cows=animal
horse=animal
chicken=animal
tiger=animal
tomato=vegetable
pea=vegetable
potato=vegetable
carrot=vegetable
I am able to get the required output if I use the following in foreach loop:
foreach key [array names myarr1] {
puts "${key}=$myarr1($key)"
}
foreach key [array names myarr2] {
puts "${key}=$myarr2($key)"
}
I am trying to create a list of array names and then loop through that list of array names and print it. If there is a better way to approach this problem, I am all ears. Thanks for the assist !

You really ought to use nested foreachs for that.
foreach arr $arr_list {
foreach key [array names $arr] {
puts "${key}=$$arr($key)"
}
}
Except that doesn't work! Why? It's easy on one level: the syntax for $ doesn't support such complexity; it really only supports a (very useful) subset of legal variable names and can't do complicated substitutions (array element names support more diverse options). We need to rewrite to use the single-argument set form as a first step:
foreach arr $arr_list {
foreach key [array names $arr] {
puts "${key}=[set [set arr]($key)]"
}
}
That works, but isn't very elegant (or fast, for that matter). It's actually better to use upvar 0 to make a local alias to the array that you're processing; variable alias from a to whatever was talked about with $arr will let us shorten things elsewhere, and it's pretty elegant in practice:
foreach arr $arr_list {
upvar 0 $arr a
foreach key [array names a] {
puts "$key=$a($key)"
}
}
You can also do things like sorting the list of element names (array names does not guarantee to return things in any particular order), putting spacing in between each of the arrays that you print out, etc. But that's the core of how to improve things. You can also use array get instead of array names and a multi-variable foreach, but then sorting the keys is more awkward (well, before Tcl 8.6's lsort gained the -stride option).

If you just want to print them, use the bundled parray proc:
foreach arr $arr_list {
parray $arr
}
which outputs
myarr1(chicken) = animal
myarr1(cows) = animal
myarr1(horse) = animal
myarr1(tiger) = animal
myarr2(carrot) = vegetable
myarr2(pea) = vegetable
myarr2(potato) = vegetable
myarr2(tomato) = vegetable

You can use the foreach command to traverse both the keys and the values in the array.
array set arr {a 1 b 2 c 3}
foreach {k v} [array get arr] {
puts $k=$v
}
References: foreach

You're on the right track with the foreach command, but to loop over the list of arrays you need to nest it:
foreach arr $arr_list {
foreach {key val} [array get arr] {
puts "$key = $val"
}
}
Also, look into the parray command, and see if that style of printing works for what you need. (I'd link it, but the site appears to be down at the moment. I'll try to remember to edit this later. Check out the command details at http://www.tcl.tk/man/tcl8.6/)

What you probably want is:
foreach arr $arr_list {
foreach key [array names $arr] {
puts "$key=[set ${arr}($key)]"
}
}
That said, you’re using arrays in a non-standard way that possibly is suboptimal. Have you looked into dictionaries? (Unfortunately, discussing how your data should be structured isn’t a Stackoverflow topic.)

Related

Print the content of a dynamic array in tcl

I am trying to create n number of arrays from a file. The arrays are getting created properly when I try to print value of an array specifically by $array_name(PAD), I am getting proper expected values. Now the issue is when I try to print the same value using a variable I am not getting the value.
Eg:
set f "BC1201_MP_PM"
puts ${f}(PAD)
The output should be the content of BC1201_MP_PM(PAD), but the output is "BC1201_MP_PM(PAD)". Can anyone please help.
Thanks in Advance!
Utkarsh
There are a few ways you can do this, some being better at some situations.
Without any context, I'd say the simplest would be to use set or a dictionary as Shawn suggested in the comments:
puts [set ${f}(PAD)]
# OR
dict set mydata BC1201_MP_PM PUB foo
puts [dict get $mydata $f PUB]
If it was a situation where you need to print all the array's contents, then you might as well use a loop with array get:
foreach {k v} [array get $f] {
puts "The value for $k is $v"
}
In a proc, you may use upvar and pass the array name to the proc like so:
proc foo {arrayName} {
upvar $arrayName localArray
puts $localArray(PAD)
}
References: set, array get, upvar

Obtaining array values in Tcl

I have tcl arrays having the names as 0,1,2,.. and the values contain meaningful information.
I want to use a single command like [array names $my_array] to get the values of the array.
Right now I only see this option,(lengthy but gets job done)
for {set index 0} {$index<[array size my_array]} {incr index} {
lappend my_list_values $my_array($index)
}
You can use array get to fetch all the elements of the array, and a foreach loop that consumes multiple elements of its list in each iteration (Plus sorting by key to get a reproducible order):
% set foo(0) a
a
% set foo(1) b
b
% set foo(2) c
c
% foreach {key val} [lsort -integer -stride 2 [array get foo]] { lappend values $val }
% puts $values
a b c
%
Or with array names:
foreach key [lsort -integer [array names foo]] { lappend values $foo($key) }
Here's a proc implementing a foreach functionality for arrays. In Tcl 8.7 this will be builtin: array for
# A note on performance: we're not saving any time with this approach.
# This is essentially `foreach name [array names ary] {...}
# We are saving memory: iterating over the names versus extracting
# them all at the beginning.
#
proc array_foreach {vars arrayName body} {
if {[llength $vars] != 2} {
error {array foreach: "vars" must be a 2 element list}
}
lassign $vars keyVar valueVar
# Using the complicated `upvar 1 $arrayName $arrayName` so that any
# error messages propagate up with the user's array name
upvar 1 $arrayName $arrayName \
$keyVar key \
$valueVar value
set sid [array startsearch $arrayName]
# If the array is modified while a search is ongoing, the searchID will
# be invalidated: wrap the commands that use $sid in a try block.
try {
while {[array anymore $arrayName $sid]} {
set key [array nextelement $arrayName $sid]
set value [set "${arrayName}($key)"]
uplevel 1 $body
}
} trap {TCL LOOKUP ARRAYSEARCH} {"" e} {
puts stderr [list $e]
dict set e -errorinfo "detected attempt to add/delete array keys while iterating"
return -options $e
} finally {
array donesearch $arrayName $sid
}
return
}
You can add this to the array ensemble:
set map [namespace ensemble configure array -map]
dict set map foreach ::array_foreach
namespace ensemble configure array -map $map
then
% array set a {foo bar baz qux}
% array foreach {key value} a {puts "key=$key, value=$value"}
key=foo, value=bar
key=baz, value=qux

Generate array and subarray dynamically (perl)

I have a several files that have contain product install information.
I am able to grep for the elements that I want (for example, "version") from the files. And I end up with something like this:
instancefile1:ABC_version=1.2.3
instancefile1:XYZ_version=2.5
instancefile2:ABC_version=3.4.5
instancefile3:XYZ_version=1.1
Where the components are named ABC or XYZ.
What I'd like to do is take this grep output and parse it through perl to build arrays on the file.
The first array would be composed of the instance number (pulled from the filenames) - above, I'd have an array that would have 1,2,3 as elements.
And inside each of those arrays would be the components that that particular instance has.
Full expected arrays and components from above:
array[0] = instancefile1 # can keep this named like the filename,
or assign name. Does not matter
array[0][0] = ABC_version=1.2.3
array[0][1] = XYZ_version=2.5
array[1] = instancefile2
array[1][0] = ABC_version=3.4.5
array[2] = instancefile3
array[2][0] = XYZ_version=1.1
(I know my notation for referencing subarrays is not correct - I'm rusty on my perl.)
How might I do that?
(I've been doing it with just bash arrays and grep and then reiterating through the initial grep output with my first array and doing another grep to fill another array - but this seems like it is going through the data more than one time, instead of building it on the fly.)
The idea is for it to build each array as it sees it. It sees "fileinstance1", it stores the values to the right in that array, as it sees it. Then if it sees "fileinstance2", it creates that array and populates with those values, all in one pass. I believe perl is the best tool for this?
Unless you can guaranteed the records with the same key will be next to each other, it's easier to start with a HoA.
my %data;
while (<>) {
chomp;
my ($key, $val) = split /:/;
push #{ $data{$key} }, $val;
}
Then convert to AoA:
my #data = values(%data);
Order-preserving:
my %data;
my #data;
while (<>) {
chomp;
my ($key, $val) = split /:/;
push #data, $data{$key} = []
if !$data{$key};
push #{ $data{$key} }, $val;
}

Using an element in a Hash of Arrays to access the key

I have a hash of arrays like this:
my #array1 = ("one", "two", "three", "four", "five");
my #array2 = ("banana", "pear", "apple");
my %hash = (
numbers => \#array1,
fruit => \#array2
);
I would like to use an element of the array to access the key. So for example, if I have "banana", I would like to print "fruit".
However when I do print $hash{banana} I get "use of unitialized value in print". How do I properly access this?
As already mentioned by Borodin in the comments, there is no direct way to accomplish this. But you could do it in the following way:
sub getKeyByValue {
my ($hashref, $val) = #_; # get sub arguments
for (keys %$hashref) {
# find key by value and give back the key
return $_ if grep $_ eq $val, #{$hashref->{$_}};
}
return undef; # value not found
}
my $key = getKeyByValue(\%hash, 'banana');
print $key;
Output: fruit
Just give the hash reference and your desired value to the subroutine getKeyByValue() and it will return the corresponding key. If the value can't be found, the subroutine will return the undefined value undef. If your data structure is really big, this trivial search is obviously not the most efficient solution.
Note: If the value banana is stored several times (under more than one key), then this subroutine will of course only return the first random match (key). You have to modify the subroutine if you are interested in all the keys under which banana is possibly stored.
There are many ways to do this, like most of the time in Perl. For example you could reverse the hash and create a new one (see example in perlfaq4).
You could create two distinct hashes:
my %hash1 = map { $_ => "numbers" } #array1;
my %hash2 = map { $_ => "fruit" } #array2;
and concatenate them:
my %hash = (%hash1, %hash2);

Perl array element comparing

I am new in Perl programming. I am trying to compare the two arrays each element. So here is my code:
#!/usr/bin/perl
use strict;
use warnings;
use v5.10.1;
my #x = ("tom","john","michell");
my #y = ("tom","john","michell","robert","ricky");
if (#x ~~ #y)
{
say "elements matched";
}
else
{
say "no elements matched";
}
When I run this I get the output
no elements matched
So I want to compare both array elements in deep and the element do not matches, those elements I want to store it in a new array. As I can now compare the only matched elements but I can't store it in a new array.
How can I store those unmatched elements in a new array?
Please someone can help me and advice.
I'd avoid smart matching in Perl - e.g. see here
If you're trying to compare the contents of $y[0] with $x[0] then this is one way to go, which puts all non-matches in an new array #keep:
use strict;
use warnings;
use feature qw/say/;
my #x = qw(tom john michell);
my #y = qw(tom john michell robert ricky);
my #keep;
for (my $i = 0; $i <$#y; $i++) {
unless ($y[$i] eq $x[$i]){
push #keep, $y[$i];
}
}
say for #keep;
Or, if you simply want to see if one name exists in the other array (and aren't interested in directly comparing elements), use two hashes:
my (%x, %y);
$x{$_}++ for #x;
$y{$_}++ for #y;
foreach (keys %y){
say if not exists $x{$_};
}
It would be well worth your while spending some time reading the Perl FAQ.
Perl FAQ 4 concerns Data Manipulation and includes the following question and answer:
How do I compute the difference of two arrays? How do I compute
the intersection of two arrays?
Use a hash. Here's code to do both and more. It assumes that each
element is unique in a given array:
my (#union, #intersection, #difference);
my %count = ();
foreach my $element (#array1, #array2) { $count{$element}++ }
foreach my $element (keys %count) {
push #union, $element;
push #{ $count{$element} > 1 ? \#intersection : \#difference }, $element;
}
Note that this is the symmetric difference, that is, all elements
in either A or in B but not in both. Think of it as an xor
operation.

Resources