The documentation (https://www.tcl.tk/man/tcl8.7/TclCmd/Tcl.htm) says:
For instance, “cmd a {*}{b [c]} d {*}{$e f {g h}}” is equivalent to “cmd a b {[c]} d {$e} f {g h}”.
But why is $e in braces after substitution? Where are those coming from (and why aren't the other arguments in the word (f and {g h})?
$e is in braces to stop it from being evaluated. f and {g h} aren't variables or in square brackets like c is, so don't need them.
These examples will also all expand to the same thing:
cmd a {*}[list b {[c]}] d {*}[list {$e} f {g h}]
cmd a {*}[list b {[c]}] d {*}[list \$e f {g h}]
cmd a {*}{b [c]} d {*}"\$e f {g h}"
Related
I have a truth table like this:
a b sel O
F F F F
F F T F
F T F F
F T T T
T F F T
T F T F
T T F T
T T T T
so O is the output column, sel is basically a selector. So when sel = F, the O will be the value of a, and if sel= T, the O will be the value of b.
So I was able to come up with an expression (without regarding b input) that correctly matches the output of sel and a, when sel = F:
a $$\lor$$ sel (for example you could check that this expression $$a v sel$$ would produce correctly all the combination of values of a, sel and O, without regarding the value of b)
And similarly for matching output of sel and b, when sel = T:
$$b ^ sel$$ (for example you could check that this expression $$b ^ sel$$ would produce correctly all the combination of values of b, sel and O, without regarding the value of a)
But now I am not sure how to come up with an expression that would correctly put together $$a v sel$$ and $$b ^ sel$$ to have a final expression that matches the truth table above.
(!s && a) || (s && b)
If s is false, the left side takes the value of a, and the right side is false (and thus ignored).
If s is true, the right side takes the value of b, and the left side is false (and thus ignored).
I am having some trouble figuring out how to iterate over space separated words/characters in a shell script. For instance I would like to iterate over a variable containing the characters in the alphabet separated by a space.
NOTE: The result should be the same even if the alphabet variable contained space separated strings instead of characters, i.e "aa bb cc ..." instead of "a b c .."
I have tried a lot of the alternatives provided from:
How to split a line into words separated by one or more spaces in bash?
Example:
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
Expected/Desired output:
1. a
2. b
3. c
and so on..
Result:
1. a b c d e f g h i j k l m n o p q r s t u v w x y z
Additional tests(without success):
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in ${alphabet[#]}; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local alphabetArray=( ${alphabet} )
local index="0"
for character in "${alphabetArray[#]}"; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
####################################################################
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local alphabetArray=( ${alphabet} )
local index="0"
for character in ${alphabetArray}; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
Could someone provide a solution on how to solve this(I would prefer a solution that iterates the alphabet variable without explicitly using an index variable, i.e $alphabet[index] )?
Thanks for your help. I discovered the error thanks to your feedback.
I thought that it was irrelevant when I posted this question but I was experimenting with functions in my .zshrc file. Hence I was using (just my assumption) the zsh interpreter and not the sh or bash interpreter.
By realizing that this could be a potential problem, I googled and found the following How to iterate through string one word at a time in zsh
So I tested the following and it works as expected:
setopt shwordsplit
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$(($index+1))
echo "$index. $character"
# Possibility to do some more stuff
done
unsetopt shwordsplit
NOTE:
index=$((++$index))
and/or
index=$(($index++))
Doesn't seem to work as I expected in zsh.
... The little gritty details, I should have used:
((++index))
or
((index++))
instead of
index=$((++$index))
Try this
IFS=$' \t\n'
local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
local index="0"
for character in $alphabet; do
index=$((++index))
echo "$index. $character"
# Possibility to do some more stuff
done
Hope it helps
This is an extract of a file from this project (full text here):
Method redirects() [ Private, ProcedureBlock = 0 ]
{
/// Mnemonics
APC d APC^%X364 q
BEL d BEL^%X364 q
CBT(%1) d CBT^%X364(%1) q
CCH d CCH^%X364 q
CHA(%1) d CHA^%X364(%1) q
CHT(%1) d CHT^%X364(%1) q
CNL(%1) d CNL^%X364(%1) q
CPL(%1) d CPL^%X364(%1) q
CPR d CPR^%X364 q
CTC(%1,%2,%3,%4,%5,%6,%7,%8,%9) d CTC^%X364(%1,%2,%3,%4,%5,%6,%7,%8,%9) q
CUB(%1) d CUB^%X364(%1) q
CUD(%1) d CUD^%X364(%1) q
CUF(%1) d CUF^%X364(%1) q
CUP(%2,%1) d CUP^%X364(%2,%1) q
CUU(%1) d CUU^%X364(%1) q
CVT(%1) d CVT^%X364(%1) q
DA d DA^%X364 q
DAQ(%1,%2,%3,%4,%5,%6,%7,%8,%9) d DAQ^%X364(%1,%2,%3,%4,%5,%6,%7,%8,%9) q
DCH(%1) d DCH^%X364(%1) q
DCS d DCS^%X364 q
DL(%1) d DL^%X364(%1) q
DMI d DMI^%X364 q
DSR(%1) d DSR^%X364(%1) q
EA(%1) d EA^%X364(%1) q
ECH(%1) d ECH^%X364(%1) q
ED(%1) d ED^%X364(%1) q
EF(%1) d EF^%X364(%1) q
EL(%1) d EL^%X364(%1) q
EMI d EMI^%X364 q
EPA d EPA^%X364 q
ESA d ESA^%X364 q
FNT d DNT^%X364 q
GSM d GSM^%X364 q
GSS d GSS^%X364 q
HPA(%1) d HPA^%X364(%1) q
HPR(%1) d HPR^%X364(%1) q
HTJ d HTJ^%X364 q
HTS d HTS^%X364 q
HVP(%1,%2) d HVP^%X364(%1,%2) q
ICH(%1) d ICH^%X364(%1) q
IL(%1) d IL^%X364(%1) q
IND d IND^%X364 q
// And others, followed by old style MAC routines
}
This is the first time I see that... And I can't find documentation on what those "mnemonics" are.
What are they? Where is the documentation for it?
This is a standard mnemonics implementation for WebTerminal inside WebTerminal itself.
To make WebTerminal work as common terminal do over WebSockets, one of the most important things is a little line of code
use $io:(/NOXY:/BREAK):"^" _ ..InitialZName
which is executed at the beginning of WebSocket server initialization, and which actually set up the name of the mnemonic space, which is equal to WebTerminal's compiled routine name (like WebTerminal.Engine.1.int).
These mnemonics (APC, BEL, etc) are a little macro programs, which user can call from the terminal using the special syntax. For example, mnemonic "CHA" is used to set the caret position:
USER > w "Pos 0", /CHA(14), "Pos 14", /CHA(35), "Pos 35"
Pos 0 Pos 14 Pos 35
In order to make user able to access all of these with this syntax, there were a need to include all standard mnemonics names into terminal routine, which is set as a mnemonic space for each client by default (because setting the default mnemonic space, which include these mnemonics breaks WebTerminal). The only solution left is to declare them inside any method (in this case, the method named "redirects") using ProcedureBlock = 0.
Talking about the syntax, in general, we have
MNEMONICNAME(%ArgByRef) do MNEMONICNAME^%SYSTEMROUTINE(%ArgByRef) quit
This just calls all of the standard mnemonics which can be found in ^%X364 system routine.
In the context of makefiles I often see in documentation/forums the term expansion thrown around quite a bit though it is rarely defined. What exactly does it mean to expand a variable or parameter when talking about makefiles?
To expand a string means to replace references to variables or function calls within the string, (e.g. $(NAME)), with the values of those things.
Consider:
FOO = a b c
BAR = $(FOO) d e
BAZ = $(BAR) f g
The value of BAZ is $(BAR) f g. If you try to use it:
$(info $(BAZ))
then Make expands the variable, which is to say it replaces $(BAZ) with the value of BAZ:
$(info $(BAR) f g)
then $(BAR) with the value of BAR:
$(info $(FOO) d e f g)
then $(FOO) with the value of FOO:
$(info a b c d e f g)
and with nothing left to expand it executes the info function and prints out "a b c d e f g".
Note that some things expand variables and others don't. For example, the assignment BAR = $(FOO) d e does not expand the $(FOO) on the right-hand side. The other kind of assignment, BAR := $(FOO) d e, does.
i have a string that random generate by a special characters (B,C,D,F,X,Z),for example to generate a following string list:
B D Z Z Z C D C Z
B D C
B Z Z Z D X
D B Z F
Z B D C C Z
B D C F Z
..........
i also have a pattern list, that is to match the generate string and return a best pattern and extract some string from the string.
string pattern
B D C [D must appear before the C >> DC]
B C F
B D C F
B X [if string have X,must be matched.]
.......
for example,
B D Z Z Z C D C Z,that have B and DC,so that can match by B D C
D B Z C F,that have B and C and F,so that can match by B C F
D B Z D F,that have B and F,so that can match by B F
.......
now,i just think about suffix array.
1.first convert a string to suffix array object.
2.loop each a pattern,that find which suffix array can be matched.
3.compare all matched patterns and get which is a best pattern.
var suffix_array=Convert a string to suffix array.
var list=new List();
for (int i=0;i<pattern length;i++){
if (suffix_array.match(pattern))
list.Add(pattern);
}
var max=list[0];
for (int i=1;i<list.length;i++){
{
if (list[i]>max)
max=list[i];
Write(list[i]);
}
i just think this method is to complex,that need to build a tree for a pattern ,and take it to match suffix array.who have a more idea?
====================update
i get a best solution now,i create a new class,that have a B,C,D,X...'s property that is array type.each property save a position that appear at the string.
now,if the B not appear at the string,we can immediately end this processing.
we can also get all the C and D position,and then compare it whether can sequential appear(DC,DCC,CCC....)
I'm not sure what programming language you are using; have you checked its capabilities with regular expressions ? If you are not familiar with these, you should be, hit Google.
var suffix_array=Convert a string to suffix array.
var best=(worst value - presumably zero - pattern);
for (int i=0;i<pattern list array length;i++){
if (suffix_array.match(pattern[i])){
if(pattern[i]>best){
best=pattern[i];
}
(add pattern[i] to list here if you still want a list of all matches)
}
}
write best;
Roughly, anyway, if I understand what you're looking for that's a slight improvement though I'm sure there may be a better solution.