I have an array [1, 2, 56, 32, 54] or something.
How do i send it to clipboard
1
2
56
32
54
I tried this.
Loop % table.MaxIndex() {
meow := table[A_Index]
Clipboard := meow"`r"
}
table := [1,2,3,4,5,6,7,8,9]
vClipboard := ClipboardAll
Clipboard := ""
Loop, % table.Count()
string := string ? string . ", " . table[A_Index] : table[A_Index]
Clipboard := string
;Do some stuff.
Clipboard := vClipboard ;Restore the Clipboard.
Your problem was trying to loop with table.MaxIndex() which will potentially give you an unexpected amount since you can have an array with 2 values but very far apart in terms of Index i.e.
table := [1]
table[93] := "String"
and also each loop was overwriting your meow value. The method you want to use is concatenate i.e.
meow := "Hello"
meow := meow . "World" or meow .= "World"
Related
I am trying write simple program that will remove all 'o' letters from the string.
Example :
I love cats
Output:
I lve cats
I wrote following code :
var
x:integer;
text:string;
text_no_o:string;
begin
text:='I love cats';
for x := 0 to Length(text) do
//writeln(Ord(text[6]));
if(Ord(text[x])=111) then
else
text_no_o[x]:=text[x];
write(text_no_o);
end.
begin
end;
end.
When text is in English program works fine .
But if i change it to Russian . It returns we question marks in console.
Code with small modifications for Russian language.
var
x:integer;
text:string;
text_no_o:string;
begin
text:='Русский язык мой родной';
for x := 0 to Length(text) do
//writeln(Ord(text[6]));
if(Ord(text[x])=190) then
else
text_no_o[x]:=text[x];
write(text_no_o);
end.
begin
end;
end.
And result in console that i receive is :
Русский язык м�й р�дн�й
I expect receive
Русский язык мй рднй
As I got the problem can be caused incorrect encoding settings in console, so i should force pascal to use CP1252 instead ANSI .
I am using Free Pascal Compiler version 3.2.0+dfsg-12 for Linux .
P.S I am not allowed to use StringReplace or Pos
Simple solution:
function Simple_StripO (Text : String) : String;
var
i : integer;
Text2 : string;
begin
Text2 := '';
for i := 1 to Length(Text) do
if Text[i] <> 'o' then
Text2 := Text2 + Text[i];
Result := Text2; // Or Simple_StripO := Text2;
end;
The string is likely to be UTF8 encoded. So the cyrillic o is encoded as two chars $d0 $be. Here you replace one $be (=190). You need to replace both chars, though you cannot just test for the value of the char, because their meaning depends of surrounding chars.
Here is a way, remembering the current state (outside of letter or after first byte)
var
c: char;
text: string;
state: (sOutside, sAfterD0);
begin
text:= 'Русский язык мой родной';
state:= sOutside;
for c in text do
begin
if state = sOutside then
begin
if c = #$D0 then // may be the start of the letter
state := sAfterD0
else
write(c); // output this char because not part of letter
end
else if state = sAfterD0 then
begin
if c = #$BE then state := sOutside // finished skipping
else
begin
// chars do not form letter so output skipped char
write(#$D0, c);
state := sOutside;
end;
end
end;
writeln;
end.
I'm looking to create an array of file names and their modified time. I can build the arrays separately. But how can I build this in a way to be like
[ [file1, modtime1], [file2, modtime2], ...]
Here is the script that builds each individual array.
modTime := []
filenames := []
counter := 1
Full_Path := "C:\Users\me\MyDocs\*.txt"
Loop, %
{
modTime[counter]:=A_LoopFileTimeModified
filenames[counter]:=A_LoopFileFullPath
counter++
}
loop % modTime.MaxIndex()
items.= modTime[A_Index] ","
StringLeft, items, items, Strlen(items)-1
msgbox % items
loop % filenames.MaxIndex()
items.= filenames[A_Index] ","
StringLeft, items, items, Strlen(items)-1
msgbox % items
return
modTime := []
counter := 1
Full_Path = % "C:\Users\me\MyDocs\*.txt"
Loop, % Full_Path
{
SplitPath, % A_LoopFileFullPath, file_name
modTime.Push([file_name, A_LoopFileTimeModified])
counter++
}
For each, element in modTime
items .= element[1] ", " element[2] "`n"
MsgBox, % RTrim(items, "`n")
I realize I might be pushing the limits of a scripting language like AHK, but I would think it should be possible to import some data structure with 16.5 million entries into an object relatively quickly. I mean the JSON file I'm trying to import is just 250MB, games load files of that size very quickly right?
I'm trying to use an AHK JSON library to import this 250MB JSON file and it's taking 15 minutes. I realize JSON probably isn't designed for large data loading, but how can I import data more quickly?
I'm open to any format or method.
Here's my code currently, some of it is commented out, which is the code used to generate and export the object to begin with:
#MaxMem 512
FileDelete, Log.txt
getTimestamp()
{
DllCall("QueryPerformanceCounter", "Int64*", timestamp)
DllCall("QueryPerformanceFrequency", "Int64*", frequency)
return Round(timestamp * 1000 / frequency)
}
splitRGBColor(RGBColor, ByRef red, ByRef green, ByRef blue)
{
red := RGBColor >> 16 & 0xFF
green := RGBColor >> 8 & 0xFF
blue := RGBColor & 0xFF
}
joinRGBColor(red, green, blue)
{
SetFormat Integer, H
red += 0
green += 0
blue += 0
SetFormat Integer, D
StringTrimLeft, red, red, 2
StringTrimLeft, green, green, 2
StringTrimLeft, blue, blue, 2
redLength := StrLen(red)
greenlength := StrLen(green)
blueLength := StrLen(blue)
if (redLength < 2) {
red = 0%red%
}
if (greenLength < 2) {
green = 0%green%
}
if (blueLength < 2) {
blue = 0%blue%
}
hex := "0xff" . red . green . blue
return hex
}
roundHexColor(color1ARGB, colorChunkSize){
;FileAppend, % "Hex: " . color1ARGB . "`n", Log.txt
splitRGBColor(color1ARGB, red, green, blue)
;FileAppend, % "Red: " . red . " Green: " . green . " Blue: " . blue . "`n", Log.txt
red := Round(red / colorChunkSize) * colorChunkSize
green := Round(green / colorChunkSize) * colorChunkSize
blue := Round(blue / colorChunkSize) * colorChunkSize
color1ARGB := joinRGBColor(red, green, blue)
;FileAppend, % "Rounded hex: " . color1ARGB . "`n", Log.txt
return color1ARGB
}
;condensedColors := {}
;loop, 255
;{
; r := A_index
; loop, 255
; {
; g := A_index
; loop, 255
; {
; b := A_index
; rgbHexRaw := joinRGBColor(r, g, b)
; rgbHexRouded := roundHexColor(rgbHexRaw, 5)
; condensedColors[rgbHexRaw] := rgbHexRounded
; }
; }
;}
;colorsJSON := JSON.Dump(condensedColors)
;FileDelete, condensedColors.json
;FileAppend, % colorsJSON, condensedColors.json
FileRead, condensedColorsJSON, condensedColors.json
condensedColors := JSON.Load(condensedColorsJSON)
testColor := 0xff3f975c
FileAppend, % "Test: " . testColor . " is rounded to " . condensedColors[testColor] . ".`n", Log.txt
runCounter := 160000
start := getTimestamp()
loop, %runCounter%
{
roundedColor := condensedColors[0xff3f975c]
}
end := getTimestamp()
duration := end - start
average := duration / runCounter
FileAppend, % "We rounded " . runCounter . " colors in " . duration . "ms, or " . average . "ms per rounded color value.`n", Log.txt
You're going to have to pre-parse the file or cut it up into chunks and process it in threads.
You can do that with a custom piece of software. Java makes threads and parsing JSON easy. If you want it to actually be fast write it in C.
If you want a pure browser based solution you'll have to roll a new database and pre-process insertions.
I am trying to capture the value in <span class="latlon"></span> at http://nominatim.openstreetmap.org/search.php?q=MK3+5JE&viewbox=-147.13%2C72.78%2C147.13%2C-55.67:
For example 51.99,-0.76 in this case:
But whenever I run my AHK script this is the output:
Why does it not read the value in the latlon field?
This is my code so far:
Loop, read, test.csv
{
Loop, parse, A_LoopReadLine, %A_Tab%
{
; Run IE
IE := ComObjCreate("InternetExplorer.Application")
IE.Visible:=True
; Copy current postcode row to clipboard
Clipboard = %A_LoopField%
Postcode = %A_LoopField%
ClipWait
; Debugging - wait 1s then check output
; Sleep 1000
; MsgBox, %Clipboard%
; Navigate to Bing Maps and paste the postcode
IE.Navigate("http://nominatim.openstreetmap.org/")
Sleep 300
Send, ^v
Send {Enter}
; Debugging - wait 1s then check output
; Sleep 1000
; IE.Navigate("javascript: alert(document.getElementsByClassName('name')[0].innerHTML)")
; IE.Navigate("javascript: alert(document.getElementsByClassName('latlon')[0].innerHTML)")
; Collect results
j := 0
i := 1
Addr := {}
while (i <= 1)
{
Sleep 1000
Addr[i] := IE.document.getElementsByClassName("name")[j].innertext
LatLon[i] := IE.document.getElementsByClassName("latlon")[j].innertext
Addr_Object := StrSplit(Addr[i], "`,")
LatLon_Object := StrSplit(LatLon[i], "`,")
If (Substr(Addr[i], 1, 2) = "MK")
{
Addr[i] := Addr_Object[2] . "," . Trim(Addr_Object[3]) . "," . PostCode . "," . LatLon_Object[1] . "," . LatLon_Object[2]
MsgBox, % Addr[i]
}
Else
{
Addr[i] := Addr_Object[1] . "," . Trim(Addr_Object[2]) . "," . PostCode . "," . LatLon_Object[1] . "," . LatLon_Object[2]
MsgBox, % Addr[i]
}
j++
i++
}
; Close IE
IE.quit()
}
}
Content of test.csv:
MK3 5JE
MK1 1AS
Would appreciate any pointers in the right direction.
I think you've got to be a bit more precise... Well, it doesn't hurt to be...
document.getElementById("searchresults").getElementsByClassName("result")[0].getElementsByClassName("latlon")[0].innerHTML
Here's an example:
You can change to loop though all of the results, but in this example, only the first result is retrieved.
SetWorkingDir, %A_scriptdir%
FileRead,data,test.csv
data := StrSplit(data,"`r`n",A_Tab)
Gui Add, ActiveX, xm w640 h480 vWB, Shell.Explorer
ComObjConnect(WB, WB_events) ; Connect WB's events to the WB_events class object.
Gui Show
WB.silent := true ;Surpress JS Error boxes
ProcessDone:=0 ; "Universal" signal
for each, item in data
{
ToolTip Loading...`nPlease wait...
PostCode:=item
WB.Navigate("http://nominatim.openstreetmap.org/search.php?q=" item) ;search it
while (!ProcessDone) {
;wait
}
ProcessDone:=0
}
return
class WB_events
{
DocumentComplete(wb, NewURL)
{
global ProcessDone
global PostCode
while (StrLen(wb.document.getElementById("searchresults").innerHTML)==0) {
;wait
}
/* Information nested as:
#searchresults
.result
.latlon
*/
numResult := wb.document.getElementById("searchresults").getElementsByClassName("result").length
addr := wb.document.getElementById("searchresults").getElementsByClassName("result")[0].getElementsByClassName("name")[0].innerHTML
coords := wb.document.getElementById("searchresults").getElementsByClassName("result")[0].getElementsByClassName("latlon")[0].innerHTML
ToolTip
MsgBox,,OpenStreenMap - First result,Address:`t%addr%`nPostal code:`t%PostCode%`nCoordinates:`t%coords%`nNumber of results:`t%numResult%`nURL: %NewURL%
ProcessDone:=1
}
}
GuiClose:
ExitApp
I'd like to create a function My`Print[args__] that prints the names of the symbols that I pass it, along with their values. The problem is that before the symbols are passed to My`Print, they're evaluated. So My`Print never gets to see the symbol names.
One solution is to surround every argument that I pass to My`Print with Unevaluated[], but this seems messy. Is there a way of defining a MACRO such that when I type My`Print[args__], the Mathematica Kernel sees My`Print[Unevaluated /# args__]?
You need to set the attribute HoldAll on your function, with SetAttribute[my`print].
Here's a possible implementation:
Clear[my`print]
SetAttributes[my`print, HoldAll]
my`print[args__] :=
Scan[
Function[x, Print[Unevaluated[x], " = ", x], {HoldAll}],
Hold[args]
]
I used lowercase names to avoid conflicts with built-ins or functions from packages.
EDIT:
Just to make it explicit: I have two functions here. One will print the value of a single symbol, and is implemented as a Function inside. You can just use this on its own if it's sufficient. The other is the actual my`print function. Note that both need to have the HoldAll attribute.
ClearAll[My`Print]
SetAttributes[My`Print, HoldAll]
My`Print[args___] :=
Do[
Print[
Extract[Hold[args], i, HoldForm], "=", List[args][[i]]
], {i, Length[List[args]]}
]
ape = 20;
nut := 20 ape;
mouse = cat + nut;
My`Print[ape, nut, mouse]
(* ==>
ape=20
nut=400
mouse=400+cat
*)
SetAttributes[MyPrint, HoldAll];
MyPrint[var_] :=
Module[
{varname = ToString[Hold[var]]},
Print[StringTake[varname, {6, StringLength[varname] - 1}],
" = ", Evaluate[var]]
]
Coming late to the party - one can use Listability to get a rather elegant (IMO) solution avoiding explicit loops or evaluation control constructs:
ClearAll[prn];
SetAttributes[prn, {HoldAll, Listable}];
prn[arg_] := Print[HoldForm[arg], " = ", arg];
prn[args___] := prn[{args}]
Stealing the test case from #Sjoerd,
In[21]:= prn[ape,nut,mouse]
During evaluation of In[21]:= ape = 20
During evaluation of In[21]:= nut = 400
During evaluation of In[21]:= mouse = 400+cat
Out[21]= {Null,Null,Null}
Here is another variation of My`Print to add to the mix:
ClearAll[My`Print]
SetAttributes[My`Print, HoldAll]
My`Print[expr_] := Print[HoldForm[expr], " = ", expr]
My`Print[exprs___] := Scan[My`Print, Hold[exprs]]
... and another...
ClearAll[My`Print]
SetAttributes[My`Print, HoldAll]
My`Print[args___] :=
Replace[
Unevaluated # CompoundExpression # args
, a_ :> Print[HoldForm[a], " = ", a]
, {1}
]
Either way, the use is the same:
$x = 23;
f[x_] := 1 + x
My`Print[$x, $x + 1, f[1]]
(* prints:
$x = 23
$x+1 = 24
f[1] = 2
*)
In addition to the other answers consider the functions DownValues, OwnValues and UpValues:
In[1] := f[x_] := x^2
In[2] := f[x_, y_] := (x + y)^2
In[3] := DownValues[f]
Out[3] = {HoldPattern[f[x_]] :> x^2, HoldPattern[f[x_, y_]] :> (x + y)^2}
http://reference.wolfram.com/mathematica/ref/DownValues.html