AutoHotkey Community

It is currently May 27th, 2012, 1:09 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: January 7th, 2010, 12:55 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 1:00 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 1:25 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 2:22 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 2:35 pm 
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 ?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 2:59 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 3:21 pm 
Offline

Joined: March 19th, 2007, 12:43 am
Posts: 532
Isn't it supposed to be
Code:
Return %log%
?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 3:31 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
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

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 3:41 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 4:08 pm 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
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 ?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 4:27 pm 
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 :oops:

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 :(

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

Thanks again
Martin


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 4:40 pm 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
Marty wrote:
yes I've tried the Introduction and Simple Examples and that makes sense,...
Then I'm not sure what your problem is :? . 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 January 7th, 2010, 4:57 pm, edited 6 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 4:42 pm 
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 :?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 4:52 pm 
Peter thank you taking the time to persevere with me. :roll:

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 ) :) :oops:

Kindest regards

Martin


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 7th, 2010, 4:56 pm 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
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)


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Leef_me, Ohnitiel, WillTroll and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group