Jump to content
Abdulla060

StringRegExp not working correctly

Recommended Posts

Abdulla060

Hello all i'm trying to make a script to list the quantity of different items stored in my inventory in a video game i'm playing the items are stored in an .csv file which i'll link an example of below this example of my code which does not work even the MsgBox won't open when i run the script

$mydata = FileOpen("testdata2.csv")
$testvar = StringRegExp( $mydata, "(.*)(?<=,Amulet)", 3)
MsgBox($MB_SYSTEMMODAL, "Something",$testvar[0])

however this code will work

$mydata = FileOpen("testdata2.csv")
$mydataline = FileReadLine( $mydata, 1)
$testvar = StringRegExp( $mydataline, "(.*)(?<=,Amulet)", 3)
MsgBox($MB_SYSTEMMODAL, "Something",$testvar[0])

it seems like i need to go through my data line by line which will defeat the whole idea of having a full array of all the matches in a single line of code

note: i'm not a professional and i only started using AutoIt a few months back so there might be something very obvious that i'm missing  

Thx in advance

Share this post


Link to post
Share on other sites
ViciousXUSMC

To see an array do not use msgbox, use ArrayDisplay()

Also FileOpen() opens the file, it does not read it.  You need FileRead()

Share this post


Link to post
Share on other sites
rudi

Hello Abdulla,

 

welcome to the forum!

 

#include <file.au3>
#include <array.au3>

$file="testdata2.csv"
$aCSV=FileReadToArray($file)

_ArrayDisplay($aCSV)

for $i = 1 to $aCSV[0]
    $aResult=StringRegExp( $aCSV[$i], "(.*)(?<=,Amulet)", 3)
    if IsArray($aResult) Then
        _ArrayDisplay($aResult)
    Else
        MsgBox(0,$i,"No array was returned!")
    EndIf
Next

Please have a look at these two pages:

 

  1. Autoit FAQ
  2. Forum rules

Have fun!

 

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
Abdulla060
4 hours ago, ViciousXUSMC said:

To see an array do not use msgbox, use ArrayDisplay()

Also FileOpen() opens the file, it does not read it.  You need FileRead()

ok that solved half of the problem i thought fileopen() was reading the file as well guess i was wrong :sweating:.

$mydata = fileread(FileOpen("testdata2.csv"))
$testvar = StringRegExp( $mydata, "(.*)(?<=,Amulet)", 3)
MsgBox($MB_SYSTEMMODAL, "Something",$testvar[0])

now this code work however now i'm getting empty row's between each match  :blink:i guess its a problem with the my pattern

Share this post


Link to post
Share on other sites
Bilgus

its the (.*) getting your empty lines change it to (.+?) and try that otherwise you need to post a sample of data from your file

+?1 or more, lazy.

Share this post


Link to post
Share on other sites
Abdulla060
2 hours ago, Bilgus said:

its the (.*) getting your empty lines change it to (.+?) and try that otherwise you need to post a sample of data from your file

+?1 or more, lazy.

Thx for the replay i ended up adding a positive lookahead which solved the problem

Share this post


Link to post
Share on other sites
rudi

Hello Abdulla,

I'd like to suggest *NOT* to use ...

$mydata = fileread(FileOpen("testdata2.csv"))

fileopen() will give you a handle that has to be used once you should need to close the file in your script. The way you did it, the handle is lost, as direktly used for fileread(), instead of saving it to some variable.

To process one line by another use FileReadLine() instead of FileRead()

$CSV="testdata2.csv"
$fCSV=FileOpen($CSV,0)

while 1
    $NextLine=FileReadLine($fCSV)
    if @error Then
        FileClose($fCSV)
        ExitLoop ; leave the WHILE loop, we are done
    Else
        ; process the next line
    EndIf
WEnd

Regards, Rudi.

  • Like 1

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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

×