Jump to content

Switching an array to add an item


Recommended Posts

I have a set of items in a file. I read line by line. Depending on the content, the data goes into arrays. How can I switch arrays conveniently? Something more like a reference to a variable from C ++. For example.

Dim $a[0], $b[0]

$t = FileReadLine

if $t = aaaa then arr = $a

if $t = bbbb then arr = $b

_ArrayAdd(arr, $t)

Is something like this possible?

Link to comment
Share on other sites

  • Moderators

mike2003,

i would do something like this:

Global $a[0], $b[0]

$hFile = FileOpen($sFile)

While 1 

    $t = FileReadLine($hFile)
    
    Switch $t
        Case -1 ; EOF
            ExitLoop
        Case "aaaa"
            _ArrayAdd($a, $t)
        Case "bbbb"
            _ArrayAdd($b, $t)
    EndSwitch

WEnd

FileClose($hFile)

M23

P.S. Click here to see how to post code.

Edited by Melba23
Typo

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

Yes but not pretty:

Local $a[0], $b[0], $arr

$t = FileReadLine

If $t = "aaaa" Then $arr = "a"
If $t = "bbbb" Then $arr = "b"

_ArrayAdd(Eval($arr), $t)

 

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

1 hour ago, jchd said:

_ArrayAdd(Eval($arr), $t)

This is very similar to what I need. But it doesn't work.

error: _ArrayAdd() called with Const or expression on ByRef-param(s).

Quote

Eval Return the value of the variable defined by a string.

Not a pointer to an array address.

Edited by mike2003
Link to comment
Share on other sites

You would need to use something like StringInStr for example:

#include <Array.au3>
Global $a[0], $b[0]
Global $sFile = @ScriptDir & "\Example.txt"
Global $hFile = FileOpen($sFile)
While 1
    $t = FileReadLine($hFile)
        If @error Then ExitLoop
    If StringInStr($t, "data1") Then
        _ArrayAdd($a, $t)
    ElseIf StringInStr($t, "data2") Then
        _ArrayAdd($b, $t)
    EndIf
WEnd
FileClose($hFile)
_ArrayDisplay($a)
_ArrayDisplay($b)

 

Link to comment
Share on other sites

18 minutes ago, Subz said:

StringInStr

But my data does not contain labels ('data1' & ' data2'), only "Marker" in the header. And that means all this will be lost. I just need to switch the target array.

Edited by mike2003
Link to comment
Share on other sites

Your posts are little confusing, in your original post you mentioned, aaaaa or bbbbb (which Melba23 referenced) you then said it wouldn't work because the data structure was different (which I was referencing).  You need to clarify what you're after or modify the examples we posted to fit your needs.

Link to comment
Share on other sites

3 hours ago, mike2003 said:

This is very similar to what I need. But it doesn't work.

error: _ArrayAdd() called with Const or expression on ByRef-param(s).

You're very right. I made the mistake to post quickly, seconds before leaving my home, and didn't engage brain when typing.

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

A more convenient (and efficient) way could be to use StringRegExp to directly build the arrays instead of populate them progressively
Concept :

$txt = FileRead

; define requirements and build the patterns
$pattern_a = ...
$pattern_b = ...

; then use regex with flag $STR_REGEXPARRAYGLOBALMATCH
; to return the 2 arrays
$a = StringRegExp($txt, $pattern_a, 3)
$b = StringRegExp($txt, $pattern_b, 3)

 

Link to comment
Share on other sites

Sorry, but either it doesn't work or I don't understand. Here is some primitive long code that I want to reduce and optimize. It has more lines and is very awkward.

$sFileRead = FileReadLine($hFileOpen)
ElseIf ($sFileRead = "mark1") Then
    $ArrMode = 1
ElseIf ($sFileRead = "mark2") Then
    $ArrMode = 2
ElseIf ($sFileRead = "mark3) Then
    $ArrMode = 3
EndIf

Switch $ArrMode
    Case 1
        _ArrayAdd($Arr1, $sFileRead)
    Case 2
        _ArrayAdd($Arr2, $sFileRead)
    Case 3
        _ArrayAdd($Arr3, $sFileRead)
EndSwitch

 

Edited by mike2003
Link to comment
Share on other sites

Maybe something like:

#include <Array.au3>
Global $Arr1[0], $Arr2[0], $Arr3[0]
Global $sFile = @ScriptDir & "\Example.txt"
Global $hFileOpen = FileOpen($sFile)
While 1
    $sFileRead = FileReadLine($hFileOpen)
        If @error Then ExitLoop
    If (StringStripWS($sFileRead,8) = "mark1") Then
        $ArrMode = 1
    ElseIf (StringStripWS($sFileRead,8) = "mark2") Then
        $ArrMode = 2
    ElseIf (StringStripWS($sFileRead,8) = "mark3") Then
        $ArrMode = 3
    ElseIf (StringStripWS($sFileRead,8) = "") Then
        ContinueLoop
    EndIf
    Switch $ArrMode
        Case 1
            _ArrayAdd($Arr1, $sFileRead)
        Case 2
            _ArrayAdd($Arr2, $sFileRead)
        Case 3
            _ArrayAdd($Arr3, $sFileRead)
    EndSwitch
WEnd
_ArrayDisplay($Arr1)
_ArrayDisplay($Arr2)
_ArrayDisplay($Arr3)

 

Link to comment
Share on other sites

@Subz  
The requirements are confusing. For the fun... :)

#include <Array.au3>

$sFile = FileRead(@ScriptDir & "\Example.txt")
$a1 = _getArray("Marker1")
$a2 = _getArray("Marker2")
$a3 = _getArray("Marker3")
_ArrayDisplay($a1)
_ArrayDisplay($a2)
_ArrayDisplay($a3)

Func _getArray($header)
    ; including the header
    Local $s = StringRegExpReplace($sFile, '(?ms).*?(' & $header & '.*?)(?=\R{2}|\z).*', "$1")
    ; excluding the header
    ;Local $s = StringRegExpReplace($sFile, '(?ms).*?' & $header & '\R(.*?)(?=\R{2}|\z).*', "$1")
    Return StringRegExp($s, '\N+', 3)
EndFunc

 

Link to comment
Share on other sites

2 hours ago, mike2003 said:

It has more lines and is very awkward.

My solution has no Switch and no multiple ArrayAdds.

It only has conditionals to evaluate the marker, which I believe are necessary.

What more were you looking to abstract?  

Code hard, but don’t hard code...

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