Sign in to follow this  
Followers 0
MrVining

FileOpen + foreign syntax + converting to an array?

15 posts in this topic

I have a file that changes somewhat frequently (at least 5 times when this section of my script runs) and I want my script to capture some data from it. The file is much longer, but I have copied the syntax completely with two entries. Every time the file is written it writes the entries in random order, so next time the script reads the file entry yyy could be above xxx. The only two variables I need to work with are "value2" and "value3". The file changes so much that I'm not even sure if it makes sense to convert it into an array, but this is a small part of my FIRST script so I'm a total newb. (I'm pretty excited about what I have learned so far, I have started to work with arrays, and my script is connecting to a remote mysql database and doing some other fun stuff).

The script will have "xxx" and needs to find the number corresponding to it. What's worse (for me) is that it doesn't need the last 4 digits of the number. So the value currently associated with "xxx" is actually "24687". I'm not afraid to read, but I'd like some advice from some experienced people to point me at the right direction.

aaa = {
["xxx_bbb::zzz"] = {
["value1"] = true,
["value2"] = "xxx",
["value3"] = 246870316,
},
["yyy_ccc::zzz"] = {
["value1"] = true,
["value2"] = "yyy",
["value3"] = 32608358,
},
}

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

MrVining,

So you want to look for the digits "24687" in the file and then extract the "value2" and "value3" items in teh lines immediately above it? Is that correct? :huh:

M23

Edit:

If that is the case then this should work:

#include <File.au3>
#include <Array.au3>

Global $aLines
_FileReadToArray("data.txt", $aLines)

$iIndex = _ArraySearch($aLines, "= 24687", 0, 0, 0, 1)
If $iIndex <> -1 Then
    $sVal_3 = StringRegExpReplace($aLines[$iIndex], '.*\["(.*)"\].*', "$1")
    $sVal_2 = StringRegExpReplace($aLines[$iIndex - 1], '.*\["(.*)"\].*', "$1")
EndIf

MsgBox(0, "Result", $sVal_2 & @CRLF & $sVal_3)

on this data:

aaa = {
["xxx_bbb::zzz"] = {
["value11"] = true,
["value12"] = "xxx",
["value13"] = 246870316,
},
["yyy_ccc::zzz"] = {
["value21"] = true,
["value22"] = "yyy",
["value23"] = 32608358,
},
}
Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

First thing you need to make sure is that the data you read is consistent.

As the file changes to frequently how will you make sure that - after you have read block 1 - the file hasn't changed and block 2 is still valid data?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

@ Melba23, I need something like:

Func NewFunc($var)
Array_Magic($var)
EndFunc

$output = NewFunc("xxx")

; $output now = 24687

@ water, The script activates the change in the file up above in a different function, so I shoulc be good there.

@ Melba23, I've started looking into that array include, I'm thinking that will be the ticket. The problem I'm running into with the code you listed is this symbol seems to be throwing me errors:

{

Share this post


Link to post
Share on other sites

Okay, I figured out my issue with your code, I didn't realize "#include <File.au3>" was not referring to the text file I was working with... /fail

Share this post


Link to post
Share on other sites

MrVining,

So does the code work as you want? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Nope, I needed the opposite of what you gave me, but I'm getting it figured out!

This scripting stuff is fun.

Share this post


Link to post
Share on other sites

MrVining,

This scripting stuff is fun

Very true! :thumbsup:

Sorry I got it the wrong way round - you know where we are if you need a pointer or two to get it working the right way. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Is there a way to start at the end of the string working backwards?

StringRegExpReplace('$string', ',', '', '1')

To take out the last comma?

Edited by MrVining

Share this post


Link to post
Share on other sites

NICE!!!! I figured out the StringTrimRight, but I didn't think to add an if!!!

Thank you thank you!

Share this post


Link to post
Share on other sites

Do you not have to use EndIf?

Share this post


Link to post
Share on other sites

No one ever seems to use it on any of the examples or posts? Just wondering what best practice is.

Share this post


Link to post
Share on other sites

The helpfile explains the options for IF quite well. There are 2 options: A one line if the performs the task defined after the "Then", or the format where the If performs anything between the IF and the endIf line.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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