Array to string in TCL - scripting

How can I convert a known array in to string in TCL? an array might have values such as root_user_appversion 10.1.3.20 and/or I just want to take out the last values out of it which 10.1.3.20 .

You can transform the array in list:
set my_list [array get my_array]
puts "last element: [lindex $my_list [expr {[llength $my_list] -1}] ]"
After that, you can easily convert your list in string with join:
set my_string [join $my_list " "]

I think you want
join [dict values [array get the_array]]
Which takes a list of alternating key / value items, filters out the value items, and joins them into a string.
Note that values with spaces will be munged: in that case you're better off with just dict values [array get the_array].
Documentation: array, dict, join

Related

Is there an equivalent of an f-string in Google Sheets?

I am making a portfolio tracker in Google Sheets and wanted to know if there is a way to link the "TICKER" column with the code in the "PRICE" column that is used to pull JSON data from Coin Gecko. I was wondering if there was an f-string like there is in Python where you can insert a variable into the string itself. Ergo, every time the Ticker column is updated the coin id will be updated within the API request string. Essentially, string interpolation
For example:
TICKER PRICE
BTC =importJSON("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids={BTC}","0.current_price")
You could use CONCATENATE for this:
https://support.google.com/docs/answer/3094123?hl=en
CONCATENATE function
Appends strings to one another.
Sample Usage
CONCATENATE("Welcome", " ", "to", " ", "Sheets!")
CONCATENATE(A1,A2,A3)
CONCATENATE(A2:B7)
Syntax
CONCATENATE(string1, [string2, ...])
string1 - The initial string.
string2 ... - [ OPTIONAL ] - Additional strings to append in sequence.
Notes
When a range with both width and height greater than 1 is specified, cell values are appended across rows rather than down columns. That is, CONCATENATE(A2:B7) is equivalent to CONCATENATE(A2,B2,A3,B3, ... , A7,B7).
See Also
SPLIT: Divides text around a specified character or string, and puts each fragment into a separate cell in the row.
JOIN: Concatenates the elements of one or more one-dimensional arrays using a specified delimiter.

Octave: is there a way of searching items in a cell array containing scalar structure with fields and how to define the conditions

I have this array in Octave :
dwnSuccess(1,1)
ans =
{
[1,1] =
scalar structure containing the fields:
site = FRED
interval = d
aard = logDir log/
dwnGrootte = log/
time = 737861.64028
and I would like to formulate conditions to find cells containing e.g. logDir in the field 'aard'.
I don't find the correct syntax. Someone knows where to find or has an example with combinations of conditions. Thanks
Assuming that you need to keep a cell array of scalar structs (instead of a struct array which makes more sense if each struct has a defined set of fieldnames), then you need to iterate the cell array to get that field and then use logical indexing to create a new cell array with the structs of interest. Like so:
aards = cellfun (#getfield, cs, {"aard"}, "UniformOutput", false);
m = strcmp(aards, "logDir"); # this must match the whole string
filter_cs2 = cs(m);
If you are interested on finding whether a string is somewhere in that field, then it's just a bit more complex:
m = ! cellfun ("isempty", strfind (aards, "logDir"));
If I understood correctly your question, then suppose you have the following cell array:
a = cell();
a{1} = struct('a', 1, 'b', 'dwn', 'c', 2);
a{2} = struct('a', 2, 'b', 'notdwn', 'c', 3);
a{3} = struct('a', 3, 'b', 'dwn', 'c', 4);
a{4} = struct('a', 4, 'b', 'dwn', 'c', 5);
I think the easiest thing to do would be to first convert it to a struct array. You can do so easily via 'sequence generator' syntax, i.e.
s = [a{:}]; % collect all cell elements as a sequence, then wrap into an array
If you are in charge of this code, then I would instead just create a struct array instead of a cell array from the very beginning.
Once you have that, you can again use a 'sequence generator' syntax on the struct array, with an appropriate function that tests for equality. In your case, you could do something like this:
strcmp( {s.b}, 'dwn' )
% ans = 1 0 1 1
s.b accesses the field 'b' in each element of the struct array, returning it as a comma separated list. Wrapping this in braces causes this sequence to become a cell array. You then pass this resulting cell array of strings into strcmp, to compare each element with the string 'dwn'.
Depending on what you want to do next, you can use that logical array as an index to your struct array to isolate only the structs that contain that value etc.
Obviously this is a quick way of doing it, if you're comfortable with generating sequences in this way. If not, the general idea stands and you're welcome to iterate using traditional for loops etc.

Removed the last element from a json[]?

I have a json[] array (_result_group) in PostgreSQL 9.4, and I want to remove its last json element (_current). I prepared with:
_result_group := (SELECT array_append(_result_group,_current));
And tried to remove with:
SELECT _result_group[1:array_length(_result_group,1) -1] INTO _result_group;
But it didn't work.
How to do this?
To remove the last element from any array (including json[]) with the means of Postgres 9.4, obviously within plpgsql code:
_result_group := _result_group[1:cardinality(_result_group)-1];
Assuming a 1-dimensional array with default subscripts starting with 1.
You get an empty array for empty array input and null for null.
According to the manual, cardinality() ...
returns the total number of elements in the array, or 0 if the array is empty
Then just take the array slice from 1 to cardinality -1.
Then again, your attempt should work as well:
SELECT _result_group[1:array_length(_result_group,1) -1] INTO _result_group;
For non-standard array subscripts see:
Normalize array subscripts for 1-dimensional array so they start with 1

How can I Select nth element from an array's 2nd dimension?

I have a 2-dimensional int array, and I'd like to get the 2nd element from every array in the 2nd dimension. So for example, I'd like to get 2,4, and 6 from the array literal '{{1,2},{3,4},{5,6}'. Is this possible? I've searched the docs but I haven't found anything that can do what I want.
unnest(arr[:][2:2]) will give you a table expression for what you want (where arr is the name of your array column)
If you want to get a 1 dimensional array of those elements, you can use array(select * from unnest(arr[:][2:2])) (because arr[:][2:2] is still a 2 dimensional one).
http://rextester.com/VLOJ18858

Combining NSArrays

I have 3 NSArrays with:
item: amount
A: 1
B: 2
C: 3
A: 2
E: 1
F: 6
C: 5
D: 1
F: 3
After "combining" these into one, I need:
A: 3
B: 2
C: 8
D: 1
E: 1
F: 9
Do I first combine all the arrays into one and then sum and remove the duplicates?
You could use an NSCountedSet. I'm not clear on the structure of the data in your arrays, but by assuming that your B: 2 means that you have two B's in the array, then something like this would work:
NSCountedSet *set = [NSCountedSet setWithCapacity:[array1 count]+[array2 count]+[array3 count]];
[set addObjectsFromArray:array1];
[set addObjectsFromArray:array2];
[set addObjectsFromArray:array3];
// Test it out!
NSUInteger countForC = [set countForObject:objC];
// countForC == 8
Instead of using a NSArray you could try using a NSMutableDictionary where the key is inherent in the objects structure. That will allow you to iterate through each of your arrays of letters and counts then query for the value with the key, get the value and add to the value, then continue processing.
One possibility would be to use:
Use predicates to extract like sets of data (by item) into separate arrays. See Collection predicates guide
Key Value Coding to sum the value field of each of the resulting arrays (by item). See KVO collection operators.
Pop the results in whatever structure you like (NSArray or NSDictionary).
There may be performance considerations to explore. Alternatively, iterate the array, pulling out matching items in a separate NSDictionary (keyed on item) and summing as you go.