lset not working properly - list

I have this issue with a lset command in my script. Wheter I issue the command to replace a specific values in the list, I end up displaying all the element of the list.
lset gamma_aa [expr int($index)] 0
instead of changing $index's value in the list to a 0 value will display the whole list in the command windows!
the List was created a single list using lrepeat
set gamma_aa [lrepeat [expr int($nbrx*$nbry)] 0.]
Did I miss something?

The lset command returns the new contents of the list, much as set returns the new contents of a variable. You can (usually) ignore that return value.
The actual setting of the value at that particular index should have happened. It might be hard to see if the value you've set it to is not particularly distinct from what was held at that index previously, or if the indexed position is a long way from the start of the list.
Also, you really don't need to do:
lset gamma_aa [expr int($index)] 0
This should work as well in all sane cases:
lset gamma_aa $index 0

Related

Batch: getting numbers between x and y from string "x123y"

I need to extract the numbers between su11b_ and .agm using batch script.
In the example below the result should be 733.
su11b_ is always the same, the numbers here will never change
733 can change, ranging from 1 - eternity (and will thus have a variable length)
Here's what I tried (and it doesn't work at all):
set "str=d:\agrcc\agrtest\server logging\su11b_733.agm"
set /A "number=str"
I'm thinking perhaps a regex thingy could extract the digits between su11b_ and .agm but I don't know how to do this in batch scripting?
You actually don't need regex if you're sure the numbers are always between su11b_ and .agm. You can just delete everything before su11b_ and delete the .agm part afterwards:
set "var=d:\agrcc\agrtest\server logging\su11b_733.agm"
set number=%var:*su11b_=%
set number=%number:.agm=%
echo %number%
This link has some more info about replacing substrings in variables in batch

set default indent in emacs

I'm trying to set the default value for the number of spaces that gets used to indent lines in C++ mode. I see a lot of answers and have tried a setting a few things (in my ~/.emacs.d):
(setq c-basic-offset 2)
In a completely separate attempt, I tried the following:
(setq-default c-basic-offset 2)
In both cases, if I use C-h v to test the value of the variable I get
Its value is 4
Original value was set-from-style
Local in buffer file.cc; global value is 2
I interpret this to mean that one of the minor-modes (I suspect the value of c-indentation-style) is overwriting the global value. So instead I set the value as follows:
(setq c-default-style "bsd"
c-basic-offset 2)
When I try to query the value of the variable I still get an output similar to above. I am able to manually set the value of the variable for buffers and it seems to work fine. Does anyone have any ideas about what I should do to make it more "permanent"?

List comparision in perl

The command "sh value" gives
A: optimal
size: 100
feature : ON
Minimum size: 0
CPU load: 100%
Done
The name-value pairs written above are parameters wit default values.
I want to compare the values corresponding to each parameter with the output of the command sh value each time its fired and verify if they match correctly. If the values don't match I need to mark the parameter for which the match doesn't happen.
How to do it ? Also, is it possible to iterate over the list with match results displayed as iterations?
I'm not going to assume the order is constant (i.e. the following code will work whether it is or not). I am going to assume the order of the fields isn't significant.
For each line of the previous output,
split the line on the first :.
Create an element in %prev_values, where the key and value are the results of split.
For each line of the current output,
split the line on the first :.
Create an element in %cur_values, where the key and value are the results of split.
For each key in %prev_values,
If the key doesn't exist in %cur_values,
This key was deleted. Print an appropriate message.
For each key in %cur_values,
If the key doesn't exists in %prev_values,
This key is a new key. Print an appropriate message.
Else
If the value of that key in %cur_values different than the one in %prev_values,
The value of this key changed. Print an appropriate message.

How do you delimit by "," instead of space in a TCL list?

So I've got a list, and in TCL the items are stored as {"1" "2" "3"}, space seperated. I'd like to convert this list to being {"1","2","3"} -- what would be an efficient way of doing this, aside from going through a foreach and lappending , to the end of each item?
Thanks!
You're confusing string representation of a list with its in-memory representation. A list in memory is just that--an ordered list of otherwise disjoint elements. If you need to "pretty print" it (for output to a terminal, typically) make a string from it, and output it then.
The simplest way to make a string in your case is to use [join] as was already suggested.
In other words, don't be deceived by the fact that the code
set L [list 1 2 3]
puts $L
outputs "1 2 3": this does not mean that "a list is stored as a string with its elements space-separated". It's just the list's default string representation used by Tcl when you ask it to implicitly make a string out of a list by passing that list to [puts] which expects a string value. (NB strictly speaking, "expects a string value" is not correct with regards to the Tcl internals, but let's ignore this for now.)
Your question doesn't really make sense to me. In TCL, lists are stored internally as strings. When you say you want to list to {"1","2","3"}, I can only assume you are referring to the external display of the list. That can be done using the join command as follows:
% set x [list 1 2 3]
1 2 3
% set z "\{\"[join $x "\","]\"\}"
{"1",2",3"}

GNU Prolog - Build up a list in a loop

I need to build a new list with a "loop". Basically i can't use recursion explicitly, so i am using append to go through lists of list.
I can get the element. Problem is i need to check this element and if something is true it returns another element i need to put back into the list. It does check correctly and it changes correctly.
Problem i am having is how do i create a completely new list.
So, if i had
[[1,1,1],[2,6,2],[3,3,3]]
I go through each element. say i get to the 6 and it changes. So i need to create a new list like so,
[[1,1,1],[2,10,2],[3,3,3]].
Right now my main problem is just creating each row. If i can create each row, i will be able to create a list of lists.
So to break this down a little more, lets just worry about [1,1,1].
I go through each element while appending the new element to a newlist. the new list is now [1,1,1]
I have this:
set(Row,Col,Bin,TheEntry,Bout) :-
append(ListLeft, [R|_], Bin),
append(ListLeft2, [C|_], R),
length(ListLeft, LenR),
length(ListLeft2,LenC),
CurrRow is LenR + 1,
CurrCol is LenC + 1,
getChar(C, Row, Col, CurrRow, CurrCol,TheEntry, NewC),
appendhere?.
I need to create a new list there with the character returned from NewC. Not sure how to do this.
Any clues?
Thanks.
To give you an idea about how to use append/3 to extract an item from a list of lists, consider the following predicate called replace/2:
replace(In, Out) :-
append(LL, [L|RL], In),
append(LE, [E|RE], L),
replaceElement(E, NewE), !,
append(LE, [NewE|RE], NewL),
append(LL, [NewL|RL], Out).
replace(In, In).
This non-recursive predicate takes, as Input, a list of lists, and backtracks to find an element E within an inner list L that can be replaced via replaceElement/2; if so, it is replaced by constructing the inner list first (NewL), then uses this new list in the construction of the new outer list (Out), as the result.
Note that this simply serves to demonstrate how to use append/3 to break apart a list of lists to retrieve individual elements as you need via backtracking, and not recursion, as requested. Once an element E is found to be replaceable by NewE via replaceElement/3, it is used in the construction of the list again using append/3 as shown.
Also note that this suggestion (which is intended to help you, not be your final answer) also happens to replace only a single element within an inner list, if any at all. If you want to do multiple replacements of the input list in a single call to replace/2 or similar using this technique, then you will almost certainly need a recursive definition, or the ability to use the global database via assert. I'm happy to be corrected if someone else can provide a definition as a counterexample.
With this example predicate replace/2, together with, say, the following fact:
replaceElement(6, 10).
Executing the following gives us your required behaviour:
1 ?- replace([[1,1,1],[2,6,2],[3,3,3]], Out).
Out = [[1, 1, 1], [2, 10, 2], [3, 3, 3]] ;
false.
If you cannot use cut (!), it is fine to omit it, but note that the second clause replace(In, In) will cause all calls to replace/2 to backtrack at least once to give you the input list back. If this behaviour is undesirable, omitting this second clause will cause replace/2 to fail outright if there is no replacement to be made.
If you cannot use recursion and have to do it with backtracking you should do something like this:
Assume Bin is a list of lists (each item is a full row)
~ Split input Bin in three parts (a list of 'left' rows, a Row, and a list of remaining rows). This can be done using append/3 with something like append(Left, [Item|Rest], Rows)
~ Now obtain the length of the 'left' rows
~ Test the length using 'is' operator to check wether the left list has Row - 1 items
~ Do the same but now with the Item, i.e. split it in three parts (LeftColums, ColumItem and Rest)
~ Test now the length against the required Column
~ Now you have the Item to change so all you need to do is rebuild a list using two appends (one to rebuild the chosen row and another to rebuild the output list).
So from your code you wouldn't use unnamed variables (_). Instead of that you have to use a named variable to be able to rebuild the new list with the item changed.