Jump to content

How can this func return 1?


typhoon
 Share

Recommended Posts

While debugging this script I got stuck on GetCard() because it was returning a 1. However, I am expecting it to return one of two things, either an array, or 0 (by default). I don't see anything in the code that would allow it to return any other value.

Func GetCard()
    $card = StringSplit(FileReadLine($_hcsv, $_linenum), ",")
    $_linenum = $_linenum + 1
    
    If $_linenum > $LINE_EOF Then
        $_linenum = $LINE_WRAP
    ElseIf $_linenum = $LINE_BOF Then
        $_loopnum = $_loopnum + 1
    EndIf
    
    If $card[$CARD_RARITY] = $_rarity AND _
       $card[$CARD_QTY] <= $_loopnum AND _
       IsFoil($card) = $_isfoil Then
        Return $card
    EndIf
EndFunc

Func IsFoil($card)
    If StringInStr($card[$CARD_NAME], $FOIL) Then Return $TRUE
EndFunc
Edited by typhoon
Link to comment
Share on other sites

  • Developers

believe you need to pass an array via byref:

Func GetCard(ByRef $card)
    $card = StringSplit(FileReadLine($_hcsv, $_linenum), ",")
    $_linenum = $_linenum + 1
    
    If $_linenum > $LINE_EOF Then
        $_linenum = $LINE_WRAP
    ElseIf $_linenum = $LINE_BOF Then
        $_loopnum = $_loopnum + 1
    EndIf
    
    If $card[$CARD_RARITY] = $_rarity AND _
       $card[$CARD_QTY] <= $_loopnum AND _
       IsFoil($card) = $_isfoil Then
       Return 1
    EndIf
EndFunc

Func IsFoil($card)
    If StringInStr($card[$CARD_NAME], $FOIL) Then Return $TRUE
EndFunc

Or just define the $card array globally and forget about the byref.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I traced the program and the $card variable is not the issue. The array is fine (StringSplit worked). The last If statement would generate a subscript error if it didn't have the array values. The problem is that, when the If statement fails naturally, as per the logic, the function should simply end, returning the default value of 0. But somehow, instead of returning 0 it is returning 1. I have noticed the difference between it returning 0 and returning 1 is whether or not it reaches the 3rd test in the If statement. If it fails before then, the function returns 0.

The trace is like this: everything up to the last If statement works as expected. The If statement tests 3 conditions. The first 2 conditions will pass, but the 3rd condition fails. The If block is skipped ($card is not returned), so the function ends, and returns 1 (but it should return 0). This 1 is simply coming from nowhere, except that I think some mechanism in the language is storing this 1 from the call to IsFoil(), because IsFoil will return $TRUE (which I defined equal to 1). I don't think this is the intended behavior and I now suspect that this is actually an AutoIt bug.

Unless someone can offer a better explanation, that's where it stands with me now.

EDIT: confirmed bug. I changed the value of $TRUE to 5, and now it returns 5. I don't see how the logic would allow for this. Or perhaps what is happening is that the Return from the IsFoil() function is being passed as the Return for GetCard()? Is that supposed to happen?

Edited by typhoon
Link to comment
Share on other sites

Guest Guidosoft

LOl. valiks got a point.

And btw, you don't need to pass an array via byref. I return arrays all the time and my scripts don't blow up. (When properly used) cause I don't put much ediot-proofs in my scripts so lol.

Link to comment
Share on other sites

LOl. valiks got a point.

And btw, you don't need to pass an array via byref. I return arrays all the time and my scripts don't blow up. (When properly used) cause I don't put much ediot-proofs in my scripts so lol.

<{POST_SNAPBACK}>

Passing arrays by value and "properly used" don't go together. Passing by value is an extremely inefficient operation.
Link to comment
Share on other sites

Guest Guidosoft

Passing arrays by value and "properly used" don't go together.  Passing by value is an extremely inefficient operation.

<{POST_SNAPBACK}>

Are you saying that this

$ORP = StringSplit("UH,UH,UH,UH,UH,UH,UH",",")
$Clorp = Snorp($ORP)

Func Snorp($ORP)
Return $ORP
EndFunc

is bad code?

Edited by Guidosoft
Link to comment
Share on other sites

This simple example should tell the tale:

Dim $a[100000]

$hTimer = TimerStart()
$res1 = ByValue($a)
MsgBox(4096, "", "By Value Results: " & TimerStop($hTimer))


$hTimer = TimerStart()
$res2 = ""
ByReference($a, $res2)
MsgBox(4096, "", "By Reference Results: " & TimerStop($hTimer))

Func ByValue($a)
    Return $a
EndFunc

Func ByReference(ByRef $a1, ByRef $a2)
    $a2 = $a1
EndFunc
Link to comment
Share on other sites

correct me if i am wrong, but that doesnt seem like a bug, more of just a something, i dont know, because msgbox(0, "blah", func1 ()) executes function 2, so im not really sure, im just thinking out loud, it just seems like, not a bug, but not something that is supposed to happen, you know?

FootbaG
Link to comment
Share on other sites

Guidosoft, that was a stupid thing to say. If all you're going to do is bitch about how slow AutoIt is, then just piss off because I'm tired of you saying that without having a clue what you're talking about. I made a mistake trying to teach you something, you'd rather just shoot your mouth off about things you don't understand.

layer, it is a bug. All the same logic and arguments here apply to this bug. Its a case where AutoIt is propagating something up through function calls when it shouldn't be. Unless the user explicitly uses Return, the return value should be what the helpfile states.

Edit: Made more apparent who each section was addressed to.

Edited by Valik
Link to comment
Share on other sites

Guest Guidosoft

This simple example should tell the tale:

Dim $a[100000]

$hTimer = TimerStart()
$res1 = ByValue($a)
MsgBox(4096, "", "By Value Results: " & TimerStop($hTimer))
$hTimer = TimerStart()
$res2 = ""
ByReference($a, $res2)
MsgBox(4096, "", "By Reference Results: " & TimerStop($hTimer))

Func ByValue($a)
    Return $a
EndFunc

Func ByReference(ByRef $a1, ByRef $a2)
    $a2 = $a1
EndFunc

<{POST_SNAPBACK}>

I see now. OK, I get it. Because in by ref it directly edits it. But in the return it passes each value at a time and it take real long.

Well, maybe they should make it so that when you return a local var, the local vars IS the thing the got the return. So it re-modifes the memory type thing bober things.

I get it now. :lmao: thankyou.

Link to comment
Share on other sites

Guest Guidosoft

Guidosoft, that was a stupid thing to say.  If all you're going to do is bitch about how slow AutoIt is, then just piss off because I'm tired of you saying that without having a clue what you're talking about.  I made a mistake trying to teach you something, you'd rather just shoot your mouth off about things you don't understand.

layer, it is a bug.  All the same logic and arguments here apply to this bug.  Its a case where AutoIt is propagating something up through function calls when it shouldn't be.  Unless the user explicitly uses Return, the return value should be what the helpfile states.

Edit: Made more apparent who each section was addressed to.

<{POST_SNAPBACK}>

And I just got done thanking you and understanding and now I read this? Edited by Guidosoft
Link to comment
Share on other sites

While debugging this script I got stuck on GetCard() because it was returning a 1. However, I am expecting it to return one of two things, either an array, or 0 (by default). I don't see anything in the code that would allow it to return any other value.

Func GetCard()
    $card = StringSplit(FileReadLine($_hcsv, $_linenum), ",")
    $_linenum = $_linenum + 1
    
    If $_linenum > $LINE_EOF Then
        $_linenum = $LINE_WRAP
    ElseIf $_linenum = $LINE_BOF Then
        $_loopnum = $_loopnum + 1
    EndIf
    
    If $card[$CARD_RARITY] = $_rarity AND _
       $card[$CARD_QTY] <= $_loopnum AND _
       IsFoil($card) = $_isfoil Then
        Return $card
    EndIf
EndFunc

Func IsFoil($card)
    If StringInStr($card[$CARD_NAME], $FOIL) Then Return $TRUE
EndFunc

<{POST_SNAPBACK}>

Yes IsFoil() failed. It is the last function called. But just like the inbuilt functions, you should have error checking. No bug in Autoit. If IsFoil was an inbuilt function within Autoit. it would be the same behaviour.
Link to comment
Share on other sites

No bug in Autoit.

<{POST_SNAPBACK}>

Do you see an explicit "Return 1" in GetCard()? Unless you see that, then AutoIt should Return 0. See the thread I linked to above concerning the exact same behavior with @error. It is a flaw that will need to be addressed. These states can not persist as they are. They present subtle problems with code that at first glance looks fine. Unless I manually say "Return <insert value>" then my function better return 0 or else the language has just made a mistake.
Link to comment
Share on other sites

I was pretty sure this thread reached its logical end at post #5.... If Valik says it's a bug, then it's a bug.

So the workaround is to explicitly put "Return 0" at the end of any function that lacks an always-executed return statement.

Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...