Jump to content
Sign in to follow this  
Sniperwolf

Should I use arraysort?

Recommended Posts

Sniperwolf

Hi there!

I am stuck with my script atm cause I don't quite know how I should do this.

I have a txt document wich I read data from lets call it A.txt for reference. A.txt contains lines of information amongst an item ID but no item title. B.txt also contains the Item ID but together with that it also contains Item title amongst other information. The documents contain only one item per line with item ID and other information related to that item.

To retrieve information from these documents I have been using filereadline and then checked with StringInStr to make sure I select the line with the item ID I want.

Now to my problem:

First I want to retrieve itemID on all items from A.txt that matches (in A.txt I have two groups of items I want to select all items within the one group) and put them in an array perhaps? Then I want to pair (assign) these itemID's with an additional Item title from B.txt using the itemID to match them. So I somehow got ItemID and Item title matched so I can retrieve ItemID if I know Item title and the other way around.

Now I want to take these items and sort them alphabetically by Item title, then I want to find out in what order a certain Item ends up by using the Item ID. Hope that description wasn't to messy :)

Visual Example:

A.txt contains (itemID, group)

1,600

2,600

3,601

4,600

5,601

B.txt contains (item title, itemID)

a_item,3

c_item,1

d_item,2

b_item,4

f_item,5

Retrieve all items from A.txt in group 600, match them with their item title from B.txt using their itemID to match them. Sort the matched items alphabetically by item title and find out on what line the item ends up at.

So if I want to find out in what order c_item ends up at I would have gotten the answer 2 (b_item, c_item, d_item are all in group 600). I was thinking I could do this with arrays and using arraysort, however I am pretty much clueless when it comes to arrays other then using stringsplit to get arrays out of a string.

Hope this makes sense to someone else, cause I am clear on what I want to do, I am just not as clear on the method. Thanks for looking guys, much appreciated.

Share this post


Link to post
Share on other sites
Melba23

Sniperwolf,

Here is one way to do what I think you want to do: :-)

Global $a600_ID[1]
Global $a600_Pairings[1][2] = [[0, 0]]

$hFile = FileOpen("A.txt", 0)
While 1
    $sLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop

    If StringInStr($sLine, "600") Then
        $aSplit = StringSplit($sLine, ",")
        $sItemID = $aSplit[1]
        _ArrayAdd($a600_ID, $sItemID)
        $a600_ID[0] += 1
    EndIf
WEnd
FileClose($hFile)

_ArrayDisplay($a600_ID)

$iCount = 1
$hFile = FileOpen("B.txt", 0)
While 1
    $sLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop

    $aSplit = StringSplit($sLine, ",")
    $iIndex = _ArraySearch($a600_ID, $aSplit[2])
    If $iIndex > 0 Then
    ; Found ID in list
        ReDim $a600_Pairings[$iCount + 1][2]
        $a600_Pairings[$iCount][0] = $aSplit[1]
        $a600_Pairings[$iCount][1] = $a600_ID[$iIndex]
        $a600_Pairings[0][0] += 1
        $iCount += 1
    EndIf
WEnd
FileClose($hFile)

_ArrayDisplay($a600_Pairings)

_ArraySort($a600_Pairings)

_ArrayDisplay($a600_Pairings)

MsgBox(0, "Result", "c_item is at position " & _ArraySearch($a600_Pairings, "c_item"))

The _ArrayDisplay lines are just so you can see what is going on - they are not vital to the functioning of the script. You should be able to modify this to meet your needs, but come back and ask if anything is unclear.

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
Sniperwolf

Thanks Melba! I got a bit on the way yesterday as well before I checked the thread so I thought I might as well try and finish what I started.

Can you guys give me some pointer on this part I made, it seems to be working and all but maybe I am doing stuff wrong, what do you guys think?

Local $a_values, $b_values, $position
$itemID=2
$count=0
$a = _FileReadToArray("a.txt",$a_values)
$b = _FileReadToArray("b.txt",$b_values)

For $i = $a_values[0] To 1 Step -1
    $a_string = String($a_values[$i])
    _ArrayDelete($a_values,$i)

    If StringInStr($a_string,"600") Then
    $a_itemID=StringSplit($a_string,",") 
    
        For $g = $b_values[0] To 1 Step -1
        $b_string = String($b_values[$g])
        
        If StringInStr($b_string,$a_itemID[1]) Then
        $b_itemName=StringSplit($b_string,",")

        _ArrayInsert($a_values,$i,$b_itemName[1]&","&$a_string)
    
        $count=$count+1
        EndIf
    Next
EndIf
Next
_ArrayDelete($a_values,0)
_ArrayInsert($a_values,0,$count)
_ArraySort($a_values)

For $i = $a_values[0] To 1 Step -1
    $a_string = String($a_values[$i])
    $itemRow = StringSplit($a_string,",")
    If $itemRow[2] = $itemID Then
        $itemPosition = $i
    EndIf
    Next
MsgBox(0,"Message","Item "&$itemID&" is on row "&$itemPosition)
Edited by Sniperwolf

Share this post


Link to post
Share on other sites
Melba23

Sniperwolf,

Looks like you got there on your own - always more satisfying that way!

As you asked, a few minor things I noticed:

1. You can replace

_ArrayDelete($a_values, 0)
_ArrayInsert($a_values, 0, $count)

with

$a_values[0] = $count

Changing the value of the element is much faster (and a lot more elegant) than deleting and replacing the element itself.

2. You can also replace

$count=$count+1

with

$count += 1

Which does the same thing, but saves wear on the keyboard and your fingers. ;-)

3. You could use the built-in function _ArraySearch to look for the item number in the array:

$iIndex = _ArraySearch($a_values, $itemID, 0, 0, 0, 1)
MsgBox(0, "Message", "Item " & $itemID & " is on row " & $iIndex)

The final '1' asks the function to return if the required value ($item_ID) is anywhere on the line - look in the Help file for more information on this parameter. But be careful how you use it - if any other part of the line holds the same characters you will get a false return. For example, if you asked for item '6' the function would find it in the first line because of the '600' on each line. That is why I used a 2-D array in my earlier code to make sure the function only looked at the correct element during the search. You could solve the problem here by adding the bounding commas:

$iIndex = _ArraySearch($a_values, "," & $itemID & ",", 0, 0, 0, 1)
MsgBox(0, "Message", "Item " & $itemID & " is on row " & $iIndex)

Then the function will only look for the ',6,' and will not return on the ',600'. I hope that is clear - ask if not.

Good luck with the rest of the project - you know where we are if you need some more help.

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
Sniperwolf

A bit shorter and a bit more nice and tidy :) Thanks for the help Melba, I learned quite a bit from what you said.

Local $a_values, $b_values, $position
$itemID=4
$count=0
$a = _FileReadToArray("a.txt",$a_values)
$b = _FileReadToArray("b.txt",$b_values) 

For $i = $a_values[0] To 1 Step -1
    $a_string = String($a_values[$i])
    _ArrayDelete($a_values,$i)

    If StringInStr($a_string,"600") Then
    $a_itemID=StringSplit($a_string,",") 
    
        For $g = $b_values[0] To 1 Step -1
        $b_string = String($b_values[$g])
        
        If StringInStr($b_string,$a_itemID[1]) Then
        $b_itemName=StringSplit($b_string,",")

        _ArrayInsert($a_values,$i,$b_itemName[1]&","&$a_string)
    
        $count += 1
        EndIf
    Next
EndIf
Next
$a_values[0] = $count
_ArraySort($a_values)

$itemPosition = _ArraySearch($a_values, ","&$itemID&",", 0, 0, 0, 1)
MsgBox(0,"Message","Item "&$itemID&" is on row "&$itemPosition)

Share this post


Link to post
Share on other sites
Melba23

Sniperwolf,

Glad I could help.

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

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.