Randomly choose 1 text-line from each of 3 lists of text-lines but instead it brings 3 blank text-lines - autohotkey

I have this code to randomly choose one line of text from each of the three lists of text lines within the same file, and copy them together to the clipboard.
It works but not properly because it brings three blank lines instead of three actual text lines.
It seems to me that the issue may be in the way this code is copying the text lines, but I'm not an expert.
Can somebody help me find where the issue is and maybe code it properly to bring the actual text lines instead of the blank ones?
My AutoHotkey is version 1.1.36.02 . Thanks in advance to everyone.
Random, rand1, 1, 4
Random, rand2, 1, 4
Random, rand3, 1, 4
; Definining 3 lists of text lines:
list1 = 1st text line, 2nd text line, 3rd text line, 4th text line
list2 = 5th text line, 6th text line, 7th text line, 8th text line
list3 = 9th text line, 10th text line, 11th text line, 12th text line
; Selecting randomly one text line from each list:
selectedLine1 := list1[rand1]
selectedLine2 := list2[rand2]
selectedLine3 := list3[rand3]
; Concatenating the 3 selected text lines and copy them to the clipboard:
clipboard = %selectedLine1% `n %selectedLine2% `n %selectedLine3%
; Seeing the result on a message box:
msgbox, Randoms: %rand1%, %rand2%, %rand3%`nSelected lines:`n%selectedLine1%`n%selectedLine2%`n%selectedLine3%

This solves the problem:
Random, rand1, 1, 4
Random, rand2, 1, 4
Random, rand3, 1, 4
; Definining 3 lists of text lines:
list1 = 1st text line, 2nd text line, 3rd text line, 4th text line
list2 = 5th text line, 6th text line, 7th text line, 8th text line
list3 = 9th text line, 10th text line, 11th text line, 12th text line
; Selecting randomly one text line from each list:
selectedLine1 := strsplit(list1,",")[rand1]
selectedLine2 := strsplit(list2,",")[rand2]
selectedLine3 := strsplit(list3,",")[rand3]
; Concatenating the 3 selected text lines and copy them to the clipboard:
clipboard = %selectedLine1% `n %selectedLine2% `n %selectedLine3%
; Seeing the result on a message box:
msgbox, Randoms: %rand1%, %rand2%, %rand3%`nSelected lines:`n%selectedLine1%`n%selectedLine2%`n%selectedLine3%

Related

In this AutohotKeys code, how to have each text-line of each list in its own line, instead of having all of them together in the same single line

I have this Autohotkeys code to randomly choose one line of text from each of the three lists of text-lines within the same file, and copy them together to the clipboard.
It works perfectly fine, but it is not practical to add new lines of long text to the code.
So I wonder if it is possible editing it to have each text-line in its own separated line in the code, instead of having all text-lines of each list in one single line, as it is right now.
For instance instead of having each list like this:
list = 1st text-line,2nd text-line,3rd text-line,4th text-line
better have them similar to something like this:
list =
1st text-line,
2nd text-line,
3rd text-line,
4th text-line
I've tried several options I know, but all I get are blank lines.
This is my current code:
Random, rand1, 1, 4
Random, rand2, 1, 4
Random, rand3, 1, 4
; Definining 3 lists of text lines:
list1 = 1st text-line,2nd text-line,3rd text-line,4th text-line
list2 = 5th text-line,6th text-line,7th text-line,8th text-line
list3 = 9th text-line,10th text-line,11th text-line,12th text-line
; Selecting randomly one text-line from each list:
selectedLine1 := strsplit(list1,",")[rand1]
selectedLine2 := strsplit(list2,",")[rand2]
selectedLine3 := strsplit(list3,",")[rand3]
; Concatenating the 3 selected text-lines and copy them to the clipboard:
clipboard = %selectedLine1%`n%selectedLine2%`n%selectedLine3%
; Seeing the result on a message box:
msgbox, Randoms: %rand1%, %rand2%, %rand3%`nSelected lines:`n%selectedLine1%`n%selectedLine2%`n%selectedLine3%
My AutoHotkey is version 1.1.36.02 . Thanks in advance for your help.
This is the solution:
Random, rand1, 1, 4
Random, rand2, 1, 4
Random, rand3, 1, 4
; Definining 3 lists of text lines:
list1 =
(
1st text-line
2nd text-line
3rd text-line
4th text-line
)
list2 =
(
5th text-line
6th text-line
7th text-line
8th text-line
)
list3 =
(
9th text-line
10th text-line
11th text-line
12th text-line
)
; Selecting randomly one text-line from each list:
selectedLine1 := strsplit(list1,"`n")[rand1]
selectedLine2 := strsplit(list2,"`n")[rand2]
selectedLine3 := strsplit(list3,"`n")[rand3]
; Concatenating the 3 selected text-lines and copy them to the clipboard:
clipboard = %selectedLine1%`n%selectedLine2%`n%selectedLine3%
; Seeing the result on a message box:
msgbox, Randoms: %rand1%, %rand2%, %rand3%`nSelected lines:`n%selectedLine1%`n%selectedLine2%`n%selectedLine3%

How to find a string of text, but exclude text from a text file?

How to exclude a line of text with “K-Address”, when there is a line of text with “Address”?
I want the line of text with the word "Address", but then I don't want the line of text if it has "K-Address" (for example). I have this code below but it grabs both lines with Address and K-Address, so I have 2 lines. I just want the one line with "Address". How can I make this happen?
myRDD.filter(line => line.contains("Address") && !(line.contains("K-Address")) )
myRDD.filter(line => line.contains(“Address”) && !(line.contains("K-Address")) )
This is correct to exclude and include text in the same line.

Split String in Array into an array autohotkey

Trying to read a CSV file in Auto Hot Key and line by line split the line by "," to pull out the last two columns of each line. Currently just trying to get the string to split into an array. I can print each line with the line
MsgBox, A_LoopReadLine but cannot split the string inside of the variable.
Have tried StringSplit and StrSplit but I am sure the syntax is incorrect.
MyArray := Object()
Loop, read, %fileop%
{
MyArray.Insert(A_LoopReadLine) ; Append this line to the array.
index := 1
MsgBox, %A_LoopReadLine%
;MyArray.
;MsgBox, % StrSplit(A_LoopReadLine ,",")
}
Loop % MyArray.Length()
MsgBox % StrSplit(MyArray[A_Index],",")
Trying to read a CSV file in Auto Hot Key and line by line split the
line by "," to pull out the last two columns of each line.
MyArray := Object()
Loop, Read, %fileop%
MyArray[A_Index]:=StrSplit(A_LoopReadLine,",")
This will store your csv file in MyArray[row][column] format. E.g to access second item in fifth row: MyArray[5][2]
for k,v in MyArray
v.RemoveAt(1,v.Length()-2)
Above will remove all but last two items from each row.
Documentation:
https://autohotkey.com/docs/commands/For.htm
https://autohotkey.com/docs/objects/Object.htm#RemoveAt_v1121+
https://autohotkey.com/docs/objects/Object.htm#Length
Edit:
And to as to why your code did not work. It kinda did.
The thing is that StrSplit() returns object, array so with below line you were trying to display object in MsgBox, this is not allowed.
MsgBox % StrSplit(MyArray[A_Index],",")
This for example would work:
MsgBox % StrSplit(MyArray[A_Index],",")[1]

Applescript to resort piles of numbers

I'm trying to resort a bunch of numbers with Applescript. I'm very new to the language and I thought I'd ask you for help.
I have a group of numbers which looks like this in my TextEdit file:
v 0.186472 0.578063 1.566364
v -0.186472 0.578063 1.566364
v 0.335649 0.578063 1.771483
What i need is a script that resorts these numbers, making it appear like this:
(0.186472, 0.578063, 1.566364),
(-0.186472, 0.578063, 1.566364),
(0.335649, 0.578063, 1.771483),
So after each number, there has to be a comma, and always the three numbers on one line have to be put into brackets (). finally there has to be another comma after every bracketed group of three and the v before every line has to be deleted.
I've only so far managed to get rid of every "v" using:
set stringToFind to "v"
set stringToReplace to ""
But now im stuck and I'm hoping for help.
To find and replace strings in AppleScript the native way is using text item delimiters. There are a fixed number of values separated by spaces (or tabs) on each line, using text item delimiters, text itemsand string concatenation we can solve your problem.
I've added an addition linefeed in front and at the back of the string to show that lines that doesn't contain 4 words are ignored.
set theString to "
v 0.186472 0.578063 1.566364
v -0.186472 0.578063 1.566364
v 0.335649 0.578063 1.771483
"
set theLines to paragraphs of theString
set oldTIDs to AppleScript's text item delimiters
repeat with i from 1 to count theLines
set AppleScript's text item delimiters to {space, tab}
if (count of text items of item i of theLines) = 4 then
set theNumbers to text items 2 thru -1 of item i of theLines
set AppleScript's text item delimiters to ", "
set item i of theLines to "(" & (theNumbers as string) & "),"
else
set item i of theLines to missing value
end if
end repeat
set theLines to text of theLines
set AppleScript's text item delimiters to linefeed
set newString to theLines as string
set AppleScript's text item delimiters to oldTIDs
return newString

Matlab - how to remove a line break when printing to screen?

There is for example a big big score
for 2 hours
and there is need to see how many more before the end of
do output on the screen of the outer loop
but the values ​​and there are many, such as 70 000
Question - how to remove a line break when printing to screen
not to receive 70 000 lines
and to see only the current display in one line?
Instead of using disp to display text to the screen, use fprintf, which requires you to enter line breaks manually.
Compare
>> disp('Hello, '), disp('World')
Hello,
World
with
>> fprintf('Hello, '), fprintf('World\n')
Hello, World
The \n at the end of 'World\n' signifies a line break (or newline as they're commonly called).
Try this function, which you can use in place of disp for a string argument. It displays to the command window, and remembers the message it has displayed. When you call it the next time, it first deletes the previous output from the command window (using ASCII backspace characters), then prints the new message.
In this way you only get to see the last message, and the command window doesn't fill up with old messages.
function teleprompt(s)
%TELEPROMPT prints to the command window, over-writing the last message
%
% TELEPROMPT(S)
% TELEPROMPT() % Terminate
%
% Input S is a string.
persistent lastMsg
if isempty(lastMsg)
lastMsg = '';
end
if nargin == 0
lastMsg = [];
fprintf('\n');
return
end
fprintf(repmat('\b', 1, numel(sprintf(lastMsg))));
fprintf(s);
lastMsg = s;