AHK Hotkeys not allowed inside function - autohotkey

I want to create a script that lets me send strings from an array, one by one at the press of a hotkey. (Press once and the first line is sent, press again and the second line is sent and so on) but my (so far limited) comprehension of AutoHotKey fails me.
This is what I have so far (”borrowed” the bit on constructiong the array from the ahk - site)
;Write to the array:
ArrayCount = 0
Loop, Read, C:\My_little_dir\test.txt{ ;test.txt contains 6-digit numbers separated only by ENTER/newline.
ArrayCount += 1 ; Keep track of how many items are in the array.
Arr_Bookings%ArrayCount% := A_LoopReadLine ; Store this line in the next array element.
}
element=1
Change(direction, element, ArrayCount){
if (direction = "next"){
;incrementing from the last element gets us back to the first element
if (element = %ArrayCount%)
{element=1}
else
{element+=1}
}
else{
if (direction = "previous"){
;decrementing from the first element gets us back to the last element
if (element=0)
{element=%ArrayCount%}
else
{element-=1}
}
}
Return Arr_Bookings%element%
}
#N::Send % Change(next,element, ArrayCount)
#B::Send % Change(previous,element, ArrayCount)
However, when I run it, I get an errormessage:
Line Text: #N::Send Change(next,element, ArrayCount)
Error:Hotkeys/hotstrings are not allowed inside functions.
I’ve checked over and over again for messed up curly braces, but to no avail (whitespace carrys no significance...right?).
Any ideas what's causing this?
Also, if you see anything else horribly wrong with this code, feel free to mention it.
Thanks in advance!
/Leo

Autohotkey does not like your indentation style. Use the Allman style.
i.e. put every bracket in its own line and don't use it if you don't have to; for example:
if (element = %ArrayCount%)
{element=1}
else
{element+=1}
braces here are completely superfluous.
I normally wouldn't do this but since i already have the code, here is your unrolled code:
;Write to the array:
ArrayCount = 0
Loop, Read, C:\My_little_dir\test.txt
{ ;test.txt contains 6-digit numbers separated only by ENTER/newline.
ArrayCount += 1 ; Keep track of how many items are in the array.
Arr_Bookings%ArrayCount% := A_LoopReadLine ; Store this line in the next array element.
}
element=1
Change(direction, element, ArrayCount)
{
if (direction = "next")
{
;incrementing from the last element gets us back to the first element
if (element = %ArrayCount%)
{
element=1
}
else
{
element+=1
}
}
else
{
if (direction = "previous")
{
;decrementing from the first element gets us back to the last element
if (element=0)
{
element=%ArrayCount%
}
else
{
element-=1
}
}
}
Return Arr_Bookings%element%
}
#N::Send Change(next,element, ArrayCount)
#B::Send Change(previous,element, ArrayCount)

Related

I am having trouble understanding why .'split' is 'stopping' at the middle of my string in dart

Thank you for taking the time to help me out. I am trying to parse a line at each ',' and adding it to a list of strings. When i attempt to print out my list, I only get 'half' of the values of my original string. I have added as much comments to my code as I can to make reading it easier.
void test(String p) {
List<String> split = [];
print('this is my p $p'); // this prints out ' this is my p Reps:3-3-3-3,Exercise:benchpress,muscleGroup:chest,Date:Mar14,2022,Weight:120'
// split = p.split(","); i tried double quotations just in case it mattered. it did not
split = p.split(',');
//tried using for each
split.forEach((element) {
//print(element); //prints out only 'Reps:3-3-3-3' 'Exercise:benchpress' 'muscleGroup:chest'
});
//i tried using at
print(split.elementAt(0)); // prints out Reps:3-3-3-3
print(split.elementAt(1)); // prints out Exercise:benchpress
print(split.elementAt(2)); // prints out muscleGroup:chest
print(split.elementAt(3)); // does not print
print(split.elementAt(4)); // does not print
print(split.elementAt(5)); // does not print
//another attempt using []
print(split[0]);// prints out Reps:3-3-3-3
print(split[1]);// prints out Exercise:benchpress
print(split[2]);// prints out muscleGroup:chest
print(split[3]); // does not print
print(split[4]); // does not print
print(split[5]); // does not print
}
(i did not use for loops so i could comment on the same line)
Since my string p=
Reps:3-3-3-3,Exercise:benchpress,muscleGroup:chest,Date:Mar14,2022,Weight:120'
I thought using .split would cause it to become
split[0]='Reps: 3-3-3-3'
split[1]='Exercise:benchpress'
split[2]='muscleGroup:chest'
split[3]='Date:Mar14'
split[4]='2022'
split[5]='Weight:120'
What am I misunderstanding? Thank you very much.

Powershell - Verify If a indexof is empty

if (($signmail.IndexOf('#')) -ne $null)
{
$signname = $signmail.Remove($signmail.IndexOf('#'))
}
Else{
$signname = $signmail
}
Hi, basicaly, I try to see if the user enter his mail address completely, or just the first part. I try here to ask If the value after a # in the mail address is not empty, to erase it and put it in the new variable. If not, the variable give directly his name to the other variable. But it not work. I always receive the StartIndex can't be below 0 error.
Anyone think of a way to make this code work for that part ?
Thanks
From the String.IndexOf() documentation:
Reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance.
So you'll want to test whether the return value is 0 or greater:
if ($signmail.IndexOf('#') -ge 0) {
$signname = $signmail.Remove($signmail.IndexOf('#'))
}
else {
$signname = $signmail
}

Randomly select an array from group of "enabled" arrays

I have a set of four arrays. I also have an option to either enable or disable 3 of the 4 arrays (one is always enabled).
Is there a way to randomly decide which array to pull a value from (of the arrays indicated as enabled)?
I originally was aiming to make a master array and just append the content of the other enabled ones into it, but it proved a little harder than expected. I figured it would be easier to simply randomly select an array to pull the single value from as long as it was "enabled".
I'm currently pulling the value with a simple statement such as
If ????? {
return promptArrayA[desiredIndexA]
} else if { ?????
return promptArrayB[desiredIndexB]
} else if { ?????
return promptArrayC[desiredIndexC]
} else {
return promptArrayD[desiredIndexD]
I'm thinking if I had a "randomizer" that chose one of the enabled arrays, then I can use that as a constraint in an If Statement.
I'm fairly new to Swift so any help is much appreciated. Thank you
You can get the array randomly by doing:
let enabledArrays = [promptArrayA, promptArrayB, promptArrayC, promptArrayD]
let randomIndex = Int.random(in: 0..<enabledArrays.count)
let randomArray = enabledArrays[randomIndex]
return randomArray[desiredIndex]

Return empty strings in woocommerce checkout form

I'd like to return empty strings on all checkout form field but one (the billing_country one).
I already know how to do it with all fields :
add_filter('woocommerce_checkout_get_value','__return_empty_string', 1, 1);
And how to do it with only one field:
add_filter('woocommerce_checkout_get_value','custom_checkout_get_value_ship_ville', 10, 2);
function custom_checkout_get_value_ship_ville( $value, $imput ){
if($imput == 'shipping_city')
$value = '';
return $value;
}
But for all but one ... I'm a little stucked.
I succeed by duplicating and adapting the previous function, but it's a lot of code for just returning empty strings.
I tried with else, elsif, switch and with logical operators, but no result.
So if someone have some clue ...
Thanks
If you want to return empty strings on every value of $imput except for one specific value you need to reverse the comparison of your second code snippet. So instead of comparing wether the $imput is equal to a value you compare wether the $imput is NOT equal to a value.
You can read up on this comparison here: http://php.net/manual/en/language.operators.comparison.php
You can also just return an empty string directly without assigning it to a variable:
add_filter('woocommerce_checkout_get_value','custom_checkout_get_value_ship_ville', 10, 2);
function custom_checkout_get_value_ship_ville( $value, $imput ){
if($imput != 'billing_country') {
return '';
}
}

What does for (;;) mean in Perl?

I was looking though a fellow developers code when I saw this ..
for (;;){
....
....
....
}
I have never seen ";;" used in a loop. What does this do exactly?
It loops forever. ';;' equates to no start value, no stop condition and no increment condition.
It is equivalent to
while (true)
{
...
}
There would usually be a conditional break; statement somewhere in the body of the loop unless it is something like a system idle loop or message pump.
All 3 parts are optional. An empty loop initialization and update is a noop. An empty terminating condition is an implicit true. It's essentially the same as
while (true) {
//...
}
Note that you it doesn't have to be all-or-nothing; you can have some part and not others.
for (init; cond; ) {
//...
}
for (; cond; update) {
//...
}
for (init; ; update) {
//...
}
Just like in C, the for loop has three sections:
a pre-loop section, which executes before the loop starts.
a continuing condition section which, while true, will keep the loop going.
a post-iteration section which is executed after each iteration of the loop body.
For example:
for (i = 1, acc = 0; i <= 10; i++)
acc += i;
will add up the numbers from 1 to 10 inclusive (in C and, assuming you use Perl syntax like $i and braces, in Perl as well).
However, nothing requires that the sections actually contain anything and, if the condition is missing, it's assumed to be true.
So the for(;;) loop basically just means: don't do any loop setup, loop forever (breaks notwithstanding) and don't do any iteration-specific processing. In other words, it's an infinite loop.
Infinite loop. A lot of the time it will be used in a thread to do some work.
boolean exit = false;
for(;;) {
if(exit) {
break;
}
// do some work
}
Infinite Loop (until you break out of it).
It's often used in place of:
while(true) { // Do something }
It's the same as
while(true) {
...
}
It loops forever.
You don't need to specify all of the parts of a for loop. For example the following loop (which contains no body, or update token) will perform a linear search of myArray
for($index = -1; $myArray[++$index] != $target;);