Jump to content

Dynamic Variables from an Array


PnD
 Share

Recommended Posts

Dear all

Currently I can be able to compare single substring to a string and then I can be able to extract  "Suite 1234566" using the code below

$a="Ave"
$a1="1234 London Square Ave Suite 1234566"
Local $iPosition = StringInStr($a1,$a)
Local $iLength = StringLen($a1)

$newposition= $iPosition + stringlen($a)

local $a2= StringRight($a1,($iLength-$newposition))
MsgBox (0,0,$a2)

my question is that if i have a text file (file.txt) that have multiple substrings on it for $a, how would I implement it to the code above?

image.png.a520964737920319d8087f4e67806b18.png

I tried to assign $a to the $arr result by row, column

local $file= @ScriptDir & "\file.txt"
local $arr= stringsplit(fileread($file),@CRLF,3)

    For $i=0 to UBound($arr)
    $a=$arr[i][0]
    Next
    MsgBox(0,0,$a)

or even _arraytostring but still not working

For $i=0 to UBound($arr)
    $a= _ArrayToString($arr,i,UBound($arr))
    Next
    MsgBox(0,0,$a)

I really appreciate your feedback.

 

 

 

Link to comment
Share on other sites

Using StringInStr can be misleading as you can search for AVE for example and the address can contain AVE somewhere else like 123 Maverick Road....

I think SRE would be more appropriate to perform a more robust search :

#include <Array.au3>

;Local $sList = StringReplace(FileRead("Text.txt"),@CRLF, "|")
Local $sList = "ALY|AVE|BCH|BLVD" ; for testing purpose
Local $sAdr = "1234 London Square Ave Suite 1234566"

Local $aExt = StringRegExp($sAdr, "(?i)\b(" & $sList & ")\b\h(.*)", 1)
_ArrayDisplay($aExt)

 

Link to comment
Share on other sites

5 hours ago, Nine said:

Using StringInStr can be misleading as you can search for AVE for example and the address can contain AVE somewhere else like 123 Maverick Road....

I think SRE would be more appropriate to perform a more robust search :

#include <Array.au3>

;Local $sList = StringReplace(FileRead("Text.txt"),@CRLF, "|")
Local $sList = "ALY|AVE|BCH|BLVD" ; for testing purpose
Local $sAdr = "1234 London Square Ave Suite 1234566"

Local $aExt = StringRegExp($sAdr, "(?i)\b(" & $sList & ")\b\h(.*)", 1)
_ArrayDisplay($aExt)

 

Thank you so much for your solution. This works excellently and accurately and prevent false positive result if using Stringinstr

Edited by PnD
Link to comment
Share on other sites

Actually, I think JockoDundee works better for our case considering the situation "Ave. or Ave," that NINE mentioned above.

Unless we either use stringreplace to remove , and .  from $sAdr or add ave, and ave. to the  $Slist, I think stringinstr requires less condition for us when we just simply put extra space to $a

Here is my code and it works flawlessly

local $file= @ScriptDir & "\file.txt"
local $arr= stringsplit(fileread($file),@CRLF,3)

$address1 = "1234 London Squavere Ave, Suite 1234566"

    For $i=0 to UBound($arr)-1

    $a = $arr[$i]
    

    Local $iPosition = StringInStr($address1," "&$a)
    if $iPosition > 0 then
    Local $iLength = StringLen($address1)

    $newposition= $iPosition + stringlen($a)+1

    local $address2= StringRight($address1,($iLength-$newposition))
    endif

    Next
    MsgBox (0,0,$address2)

  The $address2 is always "Suite 1234566" regardless , or .

Link to comment
Share on other sites

To be honest, It would just be the same. I do not think the performance is impact as the result show off pretty quickly for both ways.

Global $file= @ScriptDir & "\street.txt"
Global $sList = StringReplace(FileRead($file),@CRLF, "|")  ; Convert Enter to | example: "ALY|AVE|BCH|BLVD"
Global $sAdr1 = "1234 London Vencirtura CIR, Apt. #566999999"
Global $sAdr = StringReplace(StringReplace($sAdr1,",",""),".","")

If StringLen($sAdr)>= 35 Then

    Local $aExt = StringRegExp($sAdr, "(?i)\b(" & $sList & ")\b\h(.*)", 1)

    ;_ArrayDisplay($aExt)

    If UBound($aExt) = 0 Then
        MsgBox(0, "Contents", "Address is ok",0.5)
    else
    local $a2= $aExt[1]
    MsgBox(0,0,$a2)
    EndIf
EndIf

Thank you JockoDundee and Nine  again for your great help!

 

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