Jump to content

searching a multidimensional array


Recommended Posts

Hey everyone

First of all, I know it's probably against the rules, as it's for a game BUT it's not a tool to make the gameplay itself easier.

I wrote a script that loads weapon file to arrays and then displays stats of every weapon, I want to release the code on the official forum, as many people ask for stats of certain items.

the script reads one line of the file at a time and creates an array from it with "," delimiters. 

then the script searches for the right item name in $array[1] (0 = index, 1 = item name) and when the index is found it returns the $i from the loop, which is the line in the file, and then reads the line and parses it. 

My only problem is that it's extremely slow, it takes like 45 seconds to reach an index that is located deep in the file.

is there a way to make it search faster?

here's the searching piece of the code:

#include <file.au3>
#include <array.au3>
$searchindex = inputbox("Search","put the name you're looking for")
if $searchindex = "" Then
    Exit
EndIf
$file = "itemweapon.csv"
$lines = _FileCountLines($file)
$i = 1
progresson("Searching","Searching for "&$searchindex)
do
    $percentage = $i/$lines
    $percentage = $percentage*100
    $percentage = round($percentage, 0)
    progressset($percentage, $percentage&"%")
    $sr = filereadline($file,$i)
    if @error Then
        msgbox(0,"Error","Unable to open the file")
    EndIf
    $searcharray = stringsplit($sr,",",2)
    if $searcharray[1] = $searchindex Then
        exitloop
    EndIf
    $i = $i+1
until $i = $lines
progressoff()

once again, yes, it is for a game, but it's not a tool that will ease the gameplay in any shape, sort or form. People often dont know the stats of certain items and need to figure out the stat indexes and so on. 

if you believe it's inappropriate, please close the thread or delete it.

thank you

Link to comment
Share on other sites

  • Moderators

Tioner,

 

why even mention it's for a game?

Because having been a member here for over 2 years you seem to have missed the fact that using the "G" word here is like a red rag to a bull. So as the question has absolutely nothing to do with the game and is actually about searching arrays, why even mention it? :huh:

But at least the thread can stay open. :)

And I suggest reading the file into an array with _FileReadToArray and using _ArraySearch. Your current code is horribly inefficient as by using an index with FileReadLine you are re-reading the whole file each time. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Post sample itemweapon.csv.

It isn't game automation at all since it only interact with a passive data file regardless of game running or not.  <-- just like Melba23 found.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

 

2420195,Dragon Orb +8,3,4,25,0,0,57,0000-0000-0100-0000-0000-0000-0000-0000,3602,0,1500,0,0,1,150,0,0,2,2,0,0,1,1,3,2,1,8187,3,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,1201,1235,2020,2219,0,0,0,0,0,0,0,0,31,3,395,0,2420194,2420196,0,0,1,0,0,0,0,0,0,0

2420196,Dragon Orb +9,3,4,25,0,0,57,0000-0000-0100-0000-0000-0000-0000-0000,3602,0,1500,0,0,1,150,0,0,2,2,0,0,1,1,3,2,1,8187,3,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,1756,1802,2694,2909,0,0,0,0,0,0,0,0,31,3,395,0,2420195,2420197,0,0,1,0,0,0,0,0,0,0
2420197,Dragon Orb +10,3,4,25,0,0,57,0000-0000-0100-0000-0000-0000-0000-0000,3603,0,1500,0,0,1,150,0,0,2,2,0,0,1,1,3,2,1,8187,3,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,2623,2731,3792,4024,0,0,0,0,0,0,0,0,31,3,395,0,2420196,0,0,0,1,0,0,0,0,0,0,0
2420198,Dragon Falchion ,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1649,1897,0,0,0,0,0,0,0,0,0,0,273,77,1,0,0,2420199,0,0,1,0,0,0,0,0,0,0
2420199,Dragon Falchion +1,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1656,1906,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420198,2420200,0,0,1,0,0,0,0,0,0,0
2420200,Dragon Falchion +2,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1668,1919,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420199,2420201,0,0,1,0,0,0,0,0,0,0
2420201,Dragon Falchion +3,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1691,1944,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420200,2420202,0,0,1,0,0,0,0,0,0,0
2420202,Dragon Falchion +4,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1742,1997,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420201,2420203,0,0,1,0,0,0,0,0,0,0
2420203,Dragon Falchion +5,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1821,2078,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420202,2420204,0,0,1,0,0,0,0,0,0,0
2420204,Dragon Falchion +6,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,1943,2205,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420203,2420205,0,0,1,0,0,0,0,0,0,0
2420205,Dragon Falchion +7,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,2127,2397,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420204,2420206,0,0,1,0,0,0,0,0,0,0
2420206,Dragon Falchion +8,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,2442,2715,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420205,2420207,0,0,1,0,0,0,0,0,0,0
2420207,Dragon Falchion +9,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3604,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,2997,3282,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420206,2420208,0,0,1,0,0,0,0,0,0,0
2420208,Dragon Falchion +10,3,4,26,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3605,0,1500,0,0,1,150,0,0,2,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,3864,4211,0,0,0,0,0,0,0,0,0,0,273,77,1,0,2420207,0,0,0,1,0,0,0,0,0,0,0
2420209,Dragon Katar ,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1488,1518,0,0,0,0,0,0,0,0,0,0,154,181,1,0,0,2420210,0,0,1,0,0,0,0,0,0,0
2420210,Dragon Katar +1,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1495,1527,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420209,2420211,0,0,1,0,0,0,0,0,0,0
2420211,Dragon Katar +2,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1507,1540,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420210,2420212,0,0,1,0,0,0,0,0,0,0
2420212,Dragon Katar +3,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1530,1565,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420211,2420213,0,0,1,0,0,0,0,0,0,0
2420213,Dragon Katar +4,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1581,1618,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420212,2420214,0,0,1,0,0,0,0,0,0,0
2420214,Dragon Katar +5,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1660,1699,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420213,2420215,0,0,1,0,0,0,0,0,0,0
2420215,Dragon Katar +6,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,1782,1826,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420214,2420216,0,0,1,0,0,0,0,0,0,0
2420216,Dragon Katar +7,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,1966,2018,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420215,2420217,0,0,1,0,0,0,0,0,0,0
2420217,Dragon Katar +8,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,2281,2336,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420216,2420218,0,0,1,0,0,0,0,0,0,0
2420218,Dragon Katar +9,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3606,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,2836,2903,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420217,2420219,0,0,1,0,0,0,0,0,0,0
2420219,Dragon Katar +10,3,4,27,0,0,57,0000-0000-0000-1000-0000-0000-0000-0000,3607,0,1500,0,0,1,150,0,0,1,4,0,0,1,1,3,2,1,8339,4,1,0,0,0,5,1,1,1,0,0,0,0,0,1,20,2,20,4,20,3,20,233,350,236,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,3703,3832,0,0,0,0,0,0,0,0,0,0,154,181,1,0,2420218,0,0,0,1,0,0,0,0,0,0,0

 

here's a piece of itemweapon.csv that I want to parse. 

and here's the full code so far

and no its not nearly complete, I just couldnt stand the long search time

#include <file.au3>
#include <array.au3>
$searchindex = inputbox("Search","put the name you're looking for")
if $searchindex = "" Then
    Exit
EndIf
$file = "itemweapon.csv"
$lines = _FileCountLines($file)
$i = 1
progresson("Searching","Searching for "&$searchindex)
do
    $percentage = $i/$lines
    $percentage = $percentage*100
    $percentage = round($percentage, 0)
    progressset($percentage, $percentage&"%")
    $sr = filereadline($file,$i)
    if @error Then
        msgbox(0,"Error","Unable to open the file")
    EndIf
    $searcharray = stringsplit($sr,",",2)
    if $searcharray[1] = $searchindex Then
        exitloop
    EndIf
    $i = $i+1
until $i = $lines
progressoff()

$number = $i
$line1 = filereadline($file,$number)
$array = StringSplit($line1,",",2)
;~ _ArrayDisplay($array)

$index = $array[0]
$name = $array[1]
$class = _assignclass($array[8])
$reqlvl = $array[15]
$minatk = $array[73]
$maxatk = $array[74]
$def = $array[80]
$val1 = setitemstats($array[42])
$stat1 = $array[43]
$val2 = setitemstats($array[44])
$stat2 = $array[45]
$val3 = setitemstats($array[46])
$stat3 = $array[47]
$val4 = setitemstats($array[48])
$stat4 = $array[49]
$val5 = setitemstats($array[50])
$stat5 = $array[51]
$val6 = setitemstats($array[52])
$stat6 = $array[53]
$reinstat1 = setitemstats($array[54])
$reinval1 = $array[55]
$reinstat2 = setitemstats($array[56])
$reinval2 = $array[57]
$reinstat3 = setitemstats($array[58])
$reinval3 = $array[59]
$reinstat4 = setitemstats($array[60])
$reinval4 = $array[61]
if $stat1 <> 0 Then
    msgbox(0,"","Item name: "&$name&@CRLF&"Class: "&$class&@CRLF&"Required level: "&$reqlvl&@CRLF&"Attack: "&$minatk& " ~ "&$maxatk&@CRLF&"Defense: "&$def&@CRLF&@CRLF&"Additional stats: "&@CRLF&$val1&" "&$stat1&@CRLF&$val2&" "&$stat2&@CRLF&$val3&" "&$stat3&@CRLF&$val4&" "&$stat4&@CRLF&$val5&" "&$stat5&@CRLF&$val6&" "&$stat6)
Else
    msgbox(0,"","Item name: "&$name&@CRLF&"Class: "&$class&@CRLF&"Required level: "&$reqlvl&@CRLF&"Attack: "&$minatk& " ~ "&$maxatk&@CRLF&"Defense: "&$def)
EndIf


func setitemstats($id)
    Switch $id
        case 0
            $id = 0
        case 1
            $id = "Str"
        case 2
            $id = "Dex"
        case 3
            $id = "Heal"
        case 4
            $id = "Spr"
        case 8
            $id = "Max HP"
        case 9
            $id = "% Max HP"
        case 16
            $id = "Max MP"
        case 17
            $id = "% Max MP"
        case 72
            $id = "Defense"
        case 88
            $id = "Fire Attack"
        case 89
            $id = "Ice Attack"
        case 90
            $id = "Lightning Attack"
        case 91
            $id = "Poison Attack"
        case 92
            $id = "Curse Attack"
        case 93
            $id = "Dark Attack"
        case 233
            $id = "Critical Attack"
        case 234
            $id = "Block Rate"
        case 235
            $id = "Guard Rate"
        case 236
            $id = "Vitality"
        case 244
            $id = "All Magic Resistance"
    EndSwitch
return $id
EndFunc

func _assignclass($class)
switch $class
    Case "1111-1111-1111-1111-1111-1111-1111-1111"
        $class = "All"
    Case "0100-0000-0000-0000-0000-0000-0000-0000"
        $class = "Segita Hunter"
    Case "1000-0000-0000-0000-0000-0000-0000-0000"
        $class = "Azure Knight"
    Case "0000-0010-0000-0000-0000-0000-0000-0000"
        $class = "Aloken"
    Case "0000-0100-0000-0000-0000-0000-0000-0000"
        $class = "Bagi Warrior"
    Case "0010-0000-0000-0000-0000-0000-0000-0000"
        $class = "Incar Magician"
    Case "0000-0000-0100-0000-0000-0000-0000-0000"
        $class = "Black Wizard"
    Case "0000-0000-0000-1000-0000-0000-0000-0000"
        $class = "Half Bagi"
    Case "0000-1000-0000-0000-0000-0000-0000-0000"
        $class = "Segnale"
    Case "0000-0000-0001-0000-0000-0000-0000-0000"
        $class = "Seguripper"
    Case "0000-0000-0010-0000-0000-0000-0000-0000"
        $class = "Concerra Summoner"
    Case "0001-0000-0000-0000-0000-0000-0000-0000"
        $class = "Vicious Summoner"
    Case "0011-0000-0010-0000-0000-0000-0000-0000"
        $class = "Vicious, Concerra, Incar"
EndSwitch
return $class
EndFunc
Edited by Tioner
Link to comment
Share on other sites

Try this:

#include <file.au3>
#include <array.au3>
Local $searchindex = InputBox("Search", "put the name you're looking for")
If $searchindex = "" Then
    Exit
EndIf
Local $file = "itemweapon.csv"
Local $csv = FileRead($file)
ConsoleWrite($csv & @LF)

Local $aLine = StringRegExp($csv, "(?im)^\d+,.+? ,.*", 1)
If @error Then
    MsgBox(0, "Error", "Unable to locate item")
    Exit
EndIf
Local $array = StringSplit($aLine[0], ",", 2)
_ArrayDisplay($array)
Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

 

Try this:

#include <file.au3>
#include <array.au3>
Local $searchindex = InputBox("Search", "put the name you're looking for")
If $searchindex = "" Then
    Exit
EndIf
Local $file = "itemweapon.csv"
Local $csv = FileRead($file)
ConsoleWrite($csv & @LF)

Local $aLine = StringRegExp($csv, "(?m)^\d+,.+? ,.*", 1)
If @error Then
    MsgBox(0, "Error", "Unable to locate item")
    Exit
EndIf
Local $array = StringSplit($aLine[0], ",", 2)
_ArrayDisplay($array)

thank you for your reply, I will try your code as soon as I come home. 

I tried to analyze it, and I cant figure out what StringRegExp does, docs have a huge chunk of information about it, but I want to know what exactly the "(?m)^d+,.+? ,.*" part does. 

is there a chacne you could explain this to me?

Link to comment
Share on other sites

The pattern means:

(?m) input is multiline

^ anchor to start of line

d+ one or more digit(s)

, followed by a comma

.*? the shortest sequence of characters which don't cause the remaining of the pattern to fail

 , followed by a space and a comma

.* the remaining of the line up to but not including the newline sequence

In short this regular expression captures a line which contains the search term in the second .CSV field.

I've forgotten to make it insenvitive to case. It can surely help, so make that pattern "(?im)^d+,.+? ,.*" where the (?i) option is included.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

The pattern means:

(?m) input is multiline

^ anchor to start of line

d+ one or more digit(s)

, followed by a comma

.*? the shortest sequence of characters which don't cause the remaining of the pattern to fail

 , followed by a space and a comma

.* the remaining of the line up to but not including the newline sequence

In short this regular expression captures a line which contains the search term in the second .CSV field.

I've forgotten to make it insenvitive to case. It can surely help, so make that pattern "(?im)^d+,.+? ,.*" where the (?i) option is included.

 

thank you for your reply, I tried your method but it didnt work (I probably messed something up)

however, I made it much faster by reading the entire file, then split it into arrays using @CR delimiter, and then split each array into another bunch of arrays to search through. it gives the positive result in around 2 seconds now :)

if anyone wants to take a look at the code - here it is:

#include <file.au3>
#include <array.au3>
$searchindex = inputbox("Search","put the name you're looking for")
if $searchindex = "" Then
    Exit
EndIf
$file = "itemweapon.csv"
$lines = _FileCountLines($file)
$ff = fileread($file)
$i = 1
$array1 = stringsplit($ff,@CR,2)
progresson("Searching","Searching for "&$searchindex)
do
    $arraytosearch = StringSplit($array1[$i-1],",",2)
    $percentage = $i/$lines
    $percentage = $percentage*100
    $percentage = round($percentage, 0)
    progressset($percentage, $percentage&"%")
    if $arraytosearch[1] = $searchindex Then
        ExitLoop
    EndIf
    $i = $i+1
until $i = $lines
progressoff()
$line1 = filereadline($file,$i)

thank you again for your help - reading the entire file at a time was the way to go :)

Link to comment
Share on other sites

Oops, I'm sorry, I pasted the wrong code!

#include <file.au3>
#include <array.au3>
Local $searchindex = InputBox("Search", "put the name you're looking for")
If $searchindex = "" Then
    Exit
EndIf
Local $file = "itemweapon.csv"
Local $csv = FileRead($file)
ConsoleWrite($csv & @LF)

Local $aLine = StringRegExp($csv, "(?im)^\d+," & $searchindex & " ,.*", 1)
If @error Then
    MsgBox(0, "Error", "Unable to locate item")
    Exit
EndIf
Local $array = StringSplit($aLine[0], ",", 2)
_ArrayDisplay($array)

Please try with this one and report. It should definitely be faster.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Oops, I'm sorry, I pasted the wrong code!

#include <file.au3>
#include <array.au3>
Local $searchindex = InputBox("Search", "put the name you're looking for")
If $searchindex = "" Then
    Exit
EndIf
Local $file = "itemweapon.csv"
Local $csv = FileRead($file)
ConsoleWrite($csv & @LF)

Local $aLine = StringRegExp($csv, "(?im)^\d+," & $searchindex & " ,.*", 1)
If @error Then
    MsgBox(0, "Error", "Unable to locate item")
    Exit
EndIf
Local $array = StringSplit($aLine[0], ",", 2)
_ArrayDisplay($array)

Please try with this one and report. It should definitely be faster.

 

I tried it, Holy crap it is fast, it returns the value pretty much instantly!

hmm I tested it a bit, it returns values MUCH faster than the other method I used, however it sometimes returns error 1

Edited by Tioner
Link to comment
Share on other sites

Ah! (I'm not surprised by the power of a simple regexp)

Sorry, I kept a previous capture in the clipboard and the actual code was left in SciTe. Good to see it works for you now.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Help file on StringRegExp will tell you (almost everything).

Use this site to practice by selecting PCRE favor of regexp engine.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Help file on StringRegExp will tell you (almost everything).

Use this site to practice by selecting PCRE favor of regexp engine.

thank you very much for giving me a hand on this task - your method is the best and I'm going to use it from now on. 

glad this forum is here and people want to help eachother. I appreciate it, have a good day!

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...