AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Retrieve value from a variable within a function
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Marty
Guest





PostPosted: Thu Jan 07, 2010 11:55 am    Post subject: Retrieve value from a variable within a function Reply with quote

Hello

is it possible to retrieve data from a variable with in a function ?

Code:
updateLog(log) {
   global LogHwnd
   ControlSend,,{end},ahk_id %LogHwnd%
   Control,EditPaste,%log%,,ahk_id %LogHwnd%
   ControlSend,,{end},ahk_id %LogHwnd%
}


This is a section taken from this.

Is there any way to use the data returned in %log% out side of this function ?

I see another user trying to do something similar, but they haven't posted back how they got on.

thanks

Martin
Back to top
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Thu Jan 07, 2010 12:00 pm    Post subject: Reply with quote

Various options:
a) return var from function (look at the functions page or the return thread on ask for help a few days ago)
b) use global in your function (look at docs)
c) use byref in your function (look at docs)

http://www.autohotkey.com/docs/Functions.htm
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
Marty
Guest





PostPosted: Thu Jan 07, 2010 12:25 pm    Post subject: Reply with quote

I had looked at one thread and came up with
Code:
return (%log%)


but that always errors with -
Quote:
Error: The following variable name contains an illegal character:

The current thread will exit.
---> 92: Return,(%log%)


What is wrong with the variable name ?

kindest regards
Martin
Back to top
jsb
Guest





PostPosted: Thu Jan 07, 2010 1:22 pm    Post subject: Reply with quote

Marty wrote:
I had looked at one thread and came up with
Code:
return (%log%)


but that always errors with -
Quote:
Error: The following variable name contains an illegal character:

The current thread will exit.
---> 92: Return,(%log%)


What is wrong with the variable name ?

kindest regards
Martin
Code:

; Return,(%log%) ; this is incorrect
Return, log ; this will return the value (contents) of the variable log
Back to top
Marty
Guest





PostPosted: Thu Jan 07, 2010 1:35 pm    Post subject: Reply with quote

Thank you jsb

I tried this and got nothing!

Code:
updateLog(log) {
   global LogHwnd
   ControlSend,,{end},ahk_id %LogHwnd%
   Control,EditPaste,%log%,,ahk_id %LogHwnd%
   ControlSend,,{end},ahk_id %LogHwnd%
   Return, log
}
msgbox % log


If I do this I get output, but it's within the function.

Code:
updateLog(log) {
   global LogHwnd
   ControlSend,,{end},ahk_id %LogHwnd%
   Control,EditPaste,%log%,,ahk_id %LogHwnd%
   ControlSend,,{end},ahk_id %LogHwnd%
   msgbox % log
}


What have I done wrong ?
Back to top
jsb
Guest





PostPosted: Thu Jan 07, 2010 1:59 pm    Post subject: Reply with quote

Code:
updateLog(log) { ; log is the variable passed to this function
   global LogHwnd
   ControlSend,,{end},ahk_id %LogHwnd%
   Control,EditPaste,%log%,,ahk_id %LogHwnd%
   ControlSend,,{end},ahk_id %LogHwnd%
   Return, log
}
; msgbox % log ; incorrect if you are trying to display the value returned from the function
msgbox, % updateLog(log) ; this displays the value returned from the function
Back to top
neXt



Joined: 18 Mar 2007
Posts: 504

PostPosted: Thu Jan 07, 2010 2:21 pm    Post subject: Reply with quote

Isn't it supposed to be
Code:
Return %log%
?
_________________
simplified csv - easy way to handle csv files.
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 3700
Location: Louisville KY USA

PostPosted: Thu Jan 07, 2010 2:31 pm    Post subject: Reply with quote

no its not

jsb has posted a correct format
but um one thing
since log isnt altered within the function why not get the value without returning isnt it garbage in garbage out?


eitherway

return %log% would return whatever value log has as a variable name

so if the value of log was wood then wood better have a value like board or tree

such comments such as
Quote:
Isn't it supposed to be
Code:
Return %log%

?
are proof we need to spend more time in the manual

i suggest looking at variables and functions
_________________

We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Back to top
View user's profile Send private message
Marty
Guest





PostPosted: Thu Jan 07, 2010 2:41 pm    Post subject: Reply with quote

Thank you again JSB but it wouldn't work for me.

I've only started looking at this today, but I need some help.
This is the example code I've copied.

What I would like to do is act on the data that is retrieved from the session.

Dump seems to be the key more than updatelog, but I've no idea how to proceed with this.

I have seen other users posting questions similar to mine, but none quite the same and they've not always posted back their results.

How do I get either dump or log pushing it's data into a var that I can then act upton.

thank you for taking the time to reply.

Martin

Code:

OnExit, CleanUp
Server := "route-server.ip.att.net:23"
log := ""

;~ Debugwindow
Gui,Add,Edit,ReadOnly w800 h400 hwndLogHwnd
Gui,Show,,debug

UpdateLog("Start`n")

   Gosub, Ws2_Init

   res := expect(socket,"Username:")
   
   if (res>0) {
      send(socket,"rviews")
   } else if (res<0) {
      MsgBox Abortet by force
      ExitApp
   } else {
      MsgBox Fail!
      ExitApp
   }

   res := expect(socket,"route-server>")
   if (res>0) {
      send(socket,"ping 4.2.2.2")
   } else if (res<0) {
      MsgBox Abortet by force
      ExitApp
   } else {
      MsgBox Fail!
      ExitApp
   }


   
; Skipped, since this will do when GUI is removed :)
;~ Gosub, WS2_CleanUp
return

; Abort with escape
esc::
   e_timeout := -1
return

GuiClose:
CleanUp:
   Gosub, WS2_CleanUp
   ExitApp

WS2_CleanUp:
   WS2_CleanUp()
return

ws2_init:
   socket := WS2_Connect(Server)
   
   if ((socket<=0) || (StrLen(__WSA_ErrMsg)) || (StrLen(socket)=0)) {
      MsgBox,16,%A_ScriptName%: Sock-Error,% __WSA_ErrMsg
      Gosub, WS2_CleanUp
   }
   
   res    := WS2_AsyncSelect(socket,"dump")
   if ((Res!=0) || (StrLen(__WSA_ErrMsg))) {
      MsgBox,16,%A_ScriptName%: Async-Select-Error,% __WSA_ErrMsg
      Gosub, WS2_CleanUp
   }
Return

expect(socket,txt,timeout=5000) { ; default timeout 5000 msecs / 0 infinite wait
   global e_socket, e_txt,   e_timeout := 0
   n := 0
   Loop,
      if ((e_socket=socket) && (RegExMatch(e_txt,txt)) && (n*100<=timeout)) {
         e_socket := e_txt := ""
         return, 1
      } else if (n*100>timeout) {
         return, 0
      } else {
         if (timeout)
            n++
         sleep, 100
         if (e_timeout=-1) {
            break
         }
      }
   return -1
}

send(socket,senddata,commit="`r`n") {
   if ((StrLen(SendData)>0) && (socket!=0))
      WS2_SendData(Socket,SendData Commit)
}

dump(Socket,Txt) {
   Global
   e_socket := socket, e_txt := txt
   ;UpdateLog("<<< (" socket ") " txt)
   UpdateLog(txt)
}

updateLog(log) {
   global LogHwnd
   ControlSend,,{end},ahk_id %LogHwnd%
   Control,EditPaste,%log%,,ahk_id %LogHwnd%
   ControlSend,,{end},ahk_id %LogHwnd%
   Return, log
}
msgbox, % updateLog(log)

; Get it from here:
; http://www.autohotkey.com/forum/viewtopic.php?t=35575
#include WinSock2.ahk
Back to top
Peter



Joined: 30 Dec 2005
Posts: 448

PostPosted: Thu Jan 07, 2010 3:08 pm    Post subject: Reply with quote

Marty wrote:
How do I get either dump or log pushing it's data into a var that I can then act upton.
hugov gave 3 possible solutions. You used the first (return).
Do you understand, or did you try the Introduction and Simple Examples in Functions ?
Back to top
View user's profile Send private message
Marty
Guest





PostPosted: Thu Jan 07, 2010 3:27 pm    Post subject: Reply with quote

Hi Peter

yes I've tried the Introduction and Simple Examples

Code:
Add(x, y)
{
    return x + y   ; "Return" expects an expression.
}

MsgBox % "The answer is: " . Add(3, 2)

Var := Add(2, 3) ; The number 5 will be stored in Var.
MsgBox var =  %var%


myvar = The Quick Brown Fox
if InStr(MyVar, "fox")
    MsgBox The variable MyVar contains the word fox.


and that makes sense, but I can't seem to relate that to what I'm trying to do. I understand this is simple to others Embarassed

Today I decided to see if I could get the script I posted to allow me to use the data from within the function.. but to be honest it's well above me Sad

If anyone can see fit to explain or show me how I would appreciate it.

Thanks again
Martin
Back to top
Peter



Joined: 30 Dec 2005
Posts: 448

PostPosted: Thu Jan 07, 2010 3:40 pm    Post subject: Reply with quote

Marty wrote:
yes I've tried the Introduction and Simple Examples and that makes sense,...
Then I'm not sure what your problem is Confused . Maybe:
AHK Help file wrote:
Var := Add(2, 3) ; The number 5 will be stored in Var.
Code:
Var := updateLog(log)   ; the return value of updateLog() will be here also stored in Var

The 3 options of hugov in one piece of code:
Code:
returnVar := Function(byrefvar)
msgbox %returnvar%  %globalvar%  %byrefvar%
Function(byref par){
   global globalvar
   
   globalvar := 3
   localvar := 5
   par := localvar + globalvar
   return localvar
   
}
That should suffice.... Ihope

Last edited by Peter on Thu Jan 07, 2010 3:57 pm; edited 6 times in total
Back to top
View user's profile Send private message
Syntaxer
Guest





PostPosted: Thu Jan 07, 2010 3:42 pm    Post subject: Reply with quote

Quote:
return x + y ; "Return" expects an expression.
O-k-a-y. So what to check out next? Y E S! The AHK Help about expressions Confused
Back to top
Marty
Guest





PostPosted: Thu Jan 07, 2010 3:52 pm    Post subject: Reply with quote

Peter thank you taking the time to persevere with me. Rolling Eyes

Doing this does nothing..
Code:
updateLog(log) {
   global LogHwnd
   ControlSend,,{end},ahk_id %LogHwnd%
   Control,EditPaste,%log%,,ahk_id %LogHwnd%
   ControlSend,,{end},ahk_id %LogHwnd%
}

Var := updateLog(log)   ; the return value of updateLog() will be here also stored in Var
msgbox % Var


I know it's me who isn't doing it correctly. Any chance of another Push in the right direction ( or a bl**dy big shove ) Smile Embarassed

Kindest regards

Martin
Back to top
Peter



Joined: 30 Dec 2005
Posts: 448

PostPosted: Thu Jan 07, 2010 3:56 pm    Post subject: Reply with quote

I've added code w/ 3 options to my previous post. (AHK server reacts frequently only after a few minutes for me, so while I'm editing my post, other people replied already)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group