Concatenate strings and pass it to function in same loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Concatenate strings and pass it to function in same loop

19 Sep 2014, 10:28

hi!

Is it possible to queue/concatenate strings (filenames) and pass them in concatenated form to a function, all inside the same loop without using a timer creating a separate thread? If so how would that look like?

Code: Select all

Loop %0%  ; For each parameter (or file dropped onto a script):
{
 GivenPath := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
 ;msgbox % GivenPath
 Loop %GivenPath%, 1
 latest_file .= """" A_LoopFileLongPath """ "
 ; lots of if expressions here to loop through
 Function(latest_file)
}

Function(latest_file) {
   If latest_file =
	  return
   Else
   {
	msgbox % latest_file
	;pipe_name := "PipeExample"
   	;FileOpen("\\.\pipe\" pipe_name, "w", "UTF-8-RAW").Write(latest_file)
   }
}
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Concatenate strings and pass it to function in same loop

20 Sep 2014, 06:41

To "remember" values when calling a function, assign a static variable inside the function:

Code: Select all

for i, array in [ "dog", "cat", "fish", "bird" ]
{
    func( array )
}

func( val )
{
    static items           ; <<< initialize `items` as a static variable
    items .= val "`n"      ; <<< add/concatenate values to items variable 
    msgbox % items
}
this is just an example regarding static function variables.
you still may have some issues with your file loop.

hope this helps
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Re: Concatenate strings and pass it to function in same loop

20 Sep 2014, 09:23

Well, not quite what i was looking for.
Function toggles 4 msgbox'es with one value each.

The goal is to get the whole concatenated string in a single msgbox in function.
String and the single msgbox should look something like this in your sample:
"dog" "cat" "fish" "bird"
I imagine concatenation has to be done in the loop for that to work.

Code: Select all

for i, array in [ "dog", "cat", "fish", "bird" ]
{
    func( array )
}

func( val )
{
    static items           ; <<< initialize `items` as a static variable
    items .= """" val """ "     ; <<< add/concatenate values to items variable 
    msgbox % items
}
Last edited by zcooler on 20 Sep 2014, 10:28, edited 1 time in total.
ahcahc
Posts: 110
Joined: 25 Jul 2014, 23:55

Re: Concatenate strings and pass it to function in same loop

20 Sep 2014, 10:04

Why not msgbox after the loop was completed?
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Concatenate strings and pass it to function in same loop

20 Sep 2014, 10:52

zcooler wrote:Well, not quite what i was looking for.
you asked if it was possible to concatenation within a function,
I wanted to show an example of the string being populated.
Hence the reason for my `in function` messagebox.

Code: Select all

for i, array in [ "dog", "cat", "fish", "bird" ]
{
    list := func( array )
}

msgbox % list

func( val )
{
    static items                     ; <<< initialize `items` as a static variable
    items .= (q:="""") val q " "     ; <<< add/concatenate values to items variable
    return items
}
We are not mind readers.
Next time, please be specific when you are asking for help.
zcooler
Posts: 455
Joined: 11 Jan 2014, 04:59

Re: Concatenate strings and pass it to function in same loop

20 Sep 2014, 11:56

Sorry, I assumed I was specific with the concatenation should not take place in the function when saying:
"Is it possible to queue/concatenate strings (filenames) and pass them in concatenated form to a function"

The script im handeling is so large and packed with features it makes my head spin. Im adding functions to this script and find it hard to sequence loops and calls to functions. I do think I got it now, so thanks for your help!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 272 guests