Sign in to follow this  
Followers 0
phew

array[2] to $var

16 posts in this topic

hi,

i've got an array $array and i need $array[2] (its value is "google.com") saved in an variable $var.

i've tried following:

$var = $array[2]

which didn't work.

later i tried

#include <Array.au3>
$var = _ArrayToString($array[2], "")

which did not work either.

any solutions? :)

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Why not just use $array[2]? Kinda redundant to reassign the value to a new variable.

ye, but i need it in another var for testing reasons, already found the problem i had a spelling misstake in the variable name >_>

EDIT:

but now i get the following error when using

$auth = StringRegExp($srcv, ':(.*?)!(.*?)@(.*?) (.*?) (.*) :(.*)', 1)
            If ($auth[2] = $hostmatch) Then
                TCPSend($socket, "PRIVMSG #sh :HOST MATCHED!")
            EndIf

while $hostmatch = "mybnchost.com"

Error: Subscript used with non-Array variable.

Edited by phew

Share this post


Link to post
Share on other sites

Try this example:

Dim $array[10] = ['This', 'is', 'an', 'array', 'with', '10', 'elements', 'for', 'your', 'use']
$no = Round (Random(0, 10), 0)
$var = $array[$no]
$text = ''
For $i = 0 to 9
    $text &= '$array['&$i&'] = ' & $array[$i] & @CRLF
Next    
$text &= '$var = $array['&$no&']' & @CRLF & '$var = ' & $var
MsgBox (0, "", $text)

Share this post


Link to post
Share on other sites

Try this example:

Dim $array[10] = ['This', 'is', 'an', 'array', 'with', '10', 'elements', 'for', 'your', 'use']
$no = Round (Random(0, 10), 0)
$var = $array[$no]
$text = ''
For $i = 0 to 9
    $text &= '$array['&$i&'] = ' & $array[$i] & @CRLF
Next    
$text &= '$var = $array['&$no&']' & @CRLF & '$var = ' & $var
MsgBox (0, "", $text)
thank you, that's exactly what i was searching for :)

Share this post


Link to post
Share on other sites

ye, but i need it in another var for testing reasons, already found the problem i had a spelling misstake in the variable name >_>

EDIT:

but now i get the following error when using

$auth = StringRegExp($srcv, ':(.*?)!(.*?)@(.*?) (.*?) (.*) :(.*)', 1)
             If ($auth[2] = $hostmatch) Then
                 TCPSend($socket, "PRIVMSG #sh :HOST MATCHED!")
             EndIf

while $hostmatch = "mybnchost.com"

Error: Subscript used with non-Array variable.

What does $srcv =?

Tell us what error you recive with this:

$auth = StringRegExp($srcv, ':(.*?)!(.*?)@(.*?) (.*?) (.*) :(.*)', 1)
If Not @error = 0 Then
    Select
        Case @error = 1
            $error = "Array is invalid. No matches. "
        Case @error = 2
            $error = "Bad pattern, array is invalid. @Extended = offset of error in pattern." & @CRLF & "Extended: " & @extended
    EndSelect
    MsgBox(0, "ERROR!", $error)
Else
    If ($auth[2] = $hostmatch) Then
        TCPSend($socket, "PRIVMSG #sh :HOST MATCHED!")
    EndIf
EndIf

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

What does $srcv =?

Tell us what error you recive with this:

$auth = StringRegExp($srcv, ':(.*?)!(.*?)@(.*?) (.*?) (.*) :(.*)', 1)
If Not @error = 0 Then
    Select
        Case @error = 1
            $error = "Array is invalid. No matches. "
        Case @error = 2
            $error = "Bad pattern, array is invalid. @Extended = offset of error in pattern." & @CRLF & "Extended: " & @extended
    EndSelect
    MsgBox(0, "ERROR!", $error)
Else
    If ($auth[2] = $hostmatch) Then
        TCPSend($socket, "PRIVMSG #sh :HOST MATCHED!")
    EndIf
EndIf
"Array is invalid. No matches. " is the @error reply.

$srcv = TCPReceive($socket)

EDIT:

but _ArrayDisplay() shows me that $auth[2] is "mybnchost.com"

Edited by phew

Share this post


Link to post
Share on other sites

"Array is invalid. No matches. " is the @error reply.

$srcv = TCPReceive($socket)

That explains why there is no array. Because there was no matches. You'll need to make sure the RegExp is 110% correct... If you post an example of what $srcv is returning, one of the extremely smart RegExp guy should be able to help you :)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

That explains why there is no array. Because there was no matches. You'll need to make sure the RegExp is 110% correct... If you post an example of what $srcv is returning, one of the extremely smart RegExp guy should be able to help you :)

my regexp is:

$auth = StringRegExp($srcv, ':P.*?)!(.*?)@(.*?) (.*?) (.*) :P.*)', 1)

$srcv on connecting is:

:irc.quakenet.org NOTICE AUTH ;)** Looking up your hostname...

:irc.quakenet.org NOTICE AUTH :dance:** Found your hostname (cached)

:irc.quakenet.org 001 phew[bot]

:irc.quakenet.org 002 phew[bot] : blablabal text by quakenet

:irc.quakenet.org 003 phew[bot]

:irc.quakenet.org 004 phew[bot] : blabla bla again text by quakenet

:irc.quakenet.org 005 phew[bot]

:irc.quakenet.org 005 phew[bot]

:irc.quakenet.org 005 phew[bot]

:irc.quakenet.org 422 phew[bot] :MOTD quakenets motdtext here

:phew[bot] MODE phew[bot] :+i

$srcv when someone sends a message (PRIVMSG) to a channel:

:<NICK>!<IDENT>@<HOST> <ACTION> <CHAN> :<TEXT>

in exampulus:

:phew!phewchen@love.ganja.nl PRIVMSG #phew :!op

as you can guess, i want the bot only to accept commands given by my hostname, so that only i can control it ;)

but i'm pretty sure the regexp is correct. any help?

EDIT:

$auth[2] is <HOST> in this case

sorry for such many edits today :cheer:

Edited by phew

Share this post


Link to post
Share on other sites

$srcv = ":phew!phewchen@love.ganja.nl PRIVMSG #phew :!op"
$hostmatch = "love.ganja.nl"
$auth = StringRegExp($srcv, ':(.*?)!(.*?)@(.*?) (.*?) (.*) :(.*)', 1)
If Not @error = 0 Then
    Select
        Case @error = 1
            $error = "Array is invalid. No matches. "
        Case @error = 2
            $error = "Bad pattern, array is invalid. @Extended = offset of error in pattern." & @CRLF & "Extended: " & @extended
    EndSelect
    MsgBox(0, "ERROR!", $error)
Else
    If ($auth[2] = $hostmatch) Then
        ConsoleWrite ("PRIVMSG #sh :HOST MATCHED!"&@CRLF)
    EndIf
EndIf

That works... I'm not sure if I can see where your problem is anymore... :)

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

$srcv = ":phew!phewchen@love.ganja.nl PRIVMSG #phew :!op"
$hostmatch = "love.ganja.nl"
$auth = StringRegExp($srcv, ':(.*?)!(.*?)@(.*?) (.*?) (.*) :(.*)', 1)
If Not @error = 0 Then
    Select
        Case @error = 1
            $error = "Array is invalid. No matches. "
        Case @error = 2
            $error = "Bad pattern, array is invalid. @Extended = offset of error in pattern." & @CRLF & "Extended: " & @extended
    EndSelect
    MsgBox(0, "ERROR!", $error)
Else
    If ($auth[2] = $hostmatch) Then
        ConsoleWrite ("PRIVMSG #sh :HOST MATCHED!"&@CRLF)
    EndIf
EndIf

That works... I'm not sure if I can see where your problem is anymore... :)

i dont think this will work because

while 1

$srcv = TCPReceive($sock)

wend

$srcv is changing all the time when i receive TCP packets from the server, and there are some many packets incoming. thats why i made the regexp, $auth[2] (using my regexp) is ALWAYS the HOST (hostname.com, bla.com etc.) of the person that writes in a channel, and i want the bot to check if $auth[2] is the same as $hostmatch = "myhost.com", so that if i write it will accept my command

EDIT:

sorry by the way;

it's TCPRecv() instead of TCPReceive()

Edited by phew

Share this post


Link to post
Share on other sites

While 1
    $srcv = TCPRecv($sock)
    If $srcv <> "" Then
        $srcv = ":phew!phewchen@love.ganja.nl PRIVMSG #phew :!op"
        $hostmatch = "love.ganja.nl"
        $auth = StringRegExp($srcv, ':(.*?)!(.*?)@(.*?) (.*?) (.*) :(.*)', 1)
        If Not @error = 0 Then
            Select
                Case @error = 1
                    $error = "Array is invalid. No matches. "
                Case @error = 2
                    $error = "Bad pattern, array is invalid. @Extended = offset of error in pattern." & @CRLF & "Extended: " & @extended
            EndSelect
            MsgBox(0, "ERROR!", $error)
        Else
            If ($auth[2] = $hostmatch) Then
                ConsoleWrite("PRIVMSG #sh :HOST MATCHED!" & @CRLF)
            EndIf
        EndIf
    EndIf
WEnd

I think that should work...

Share this post


Link to post
Share on other sites

While 1
    $srcv = TCPRecv($sock)
    If $srcv <> "" Then
        $srcv = ":phew!phewchen@love.ganja.nl PRIVMSG #phew :!op"
        $hostmatch = "love.ganja.nl"
        $auth = StringRegExp($srcv, ':(.*?)!(.*?)@(.*?) (.*?) (.*) :(.*)', 1)
        If Not @error = 0 Then
            Select
                Case @error = 1
                    $error = "Array is invalid. No matches. "
                Case @error = 2
                    $error = "Bad pattern, array is invalid. @Extended = offset of error in pattern." & @CRLF & "Extended: " & @extended
            EndSelect
            MsgBox(0, "ERROR!", $error)
        Else
            If ($auth[2] = $hostmatch) Then
                ConsoleWrite("PRIVMSG #sh :HOST MATCHED!" & @CRLF)
            EndIf
        EndIf
    EndIf
WEnd

I think that should work...

no it can't work, it's because of the RAW is changing everytime :) different channels, different ident, different nick, diff text, only my host will be constant.

but thank you very much for helping, perhaps i'll get an answer until tomorrow but now i g2g ;)

cya

Share this post


Link to post
Share on other sites

Cant you just do;

$auth = StringRegExp($srcv, '@(.*?) ', 1)

This only match the host and you can call it with $auth[0] rather that $auth[2] since the host name will always be after @.

no because then i have not the cmd-line <TEXT> in my array. but already fixed the problem, i couldnt save $array[2] in an array because while where was no text written in chan, $auth[2] was empty and $auth was 0, so i received an error, just fixed it by adding if ($auth <> 0) Then ... :)

but thank you for the help guys

Share this post


Link to post
Share on other sites

no because then i have not the cmd-line <TEXT> in my array. but already fixed the problem, i couldnt save $array[2] in an array because while where was no text written in chan, $auth[2] was empty and $auth was 0, so i received an error, just fixed it by adding if ($auth <> 0) Then ... :)

but thank you for the help guys

Ah, gotcha! ;)

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Share this post


Link to post
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
Sign in to follow this  
Followers 0