Jump to content

Reading Strings


Bert
 Share

Recommended Posts

RegExp's are tricky, but can save you lots of time when you know them by hart (unfortunately I'm a bit rusty :think: ) so it is worth doing some research on.

Does your data contain other caracters than a-z A-Z 0-9 space and | ???

Link to comment
Share on other sites

From the look of it, I've got "-", spaces, "(" and ")", ":", and "_", depending on the entry.

So you have to modift the pattern to include all those characters:

#include <Array.au3>
$data = "|APPLE POWERMAC G4|APPLE-IMAC|COMPAQ DC5000|COMPAQ DESKPRO 2000|COMPAQ DESKPRO 4000|COMPAQ DESKPRO 5100|COMPAQ DESKPRO 6000|COMPAQ DESKPRO EN|COMPAQ DESKPRO EN SFF|COMPAQ DESKPRO EN-CMT|COMPAQ DESKPRO EP|COMPAQ EVO D500|COMPAQ EVO D510|COMPAQ EVO D530|COMPAQ EVO D530 CMT|COMPAQ EVO W6000|COMPAQ EVO W8000|COMPAQ PROLIANT HD|COMPAQ PROLIANT ML330|COMPAQ PROLIANT ML380|COMPAQ PROSIGNIA|COMPAQ SP 750|COMPAQ XW6000|DELL OPTIPLEX|HP DC7100|IBM PC 300GL|IBM PC 300PL|UNISYS TELLER STATION|"
$start = "[|]"; [^a-zA-Z0-9 :()-_]
$end = "[|]"; [^a-zA-Z0-9 :()-_]
$pattern = "(" & $start & "[^C][a-zA-Z0-9 :()-_]*" & $end & ")"
do 
    $data = StringRegExpReplace($data,$pattern,"|")
    $res = @extended
until $res <= 0

ConsoleWrite("@error:=" & @error & ", @Extended:=" & @extended & @LF)
ConsoleWrite("$arr:=" & $data & @LF)

Note that the delimiting pattern [^a-zA-Z0-9 :()-_] is sort of a (stupid) replacement for [|]. The code above use [|] but I'm not shure if it returns what I expect it to.

Could you try it now. If it does not exlude waht you want or exlude to much, provide some samples please.

Link to comment
Share on other sites

I've been getting quite into RegExp's, and yes, they are very tricky at times.

Here, give this a whirl:

$data = "|APPLE POWERMAC G4|APPLE-IMAC|COMPAQ DC5000|COMPAQ DESKPRO 2000|COMPAQ DESKPRO 4000|COMPAQ DESKPRO 5100|COMPAQ DESKPRO 6000|COMPAQ DESKPRO EN|COMPAQ DESKPRO EN SFF|COMPAQ DESKPRO EN-CMT|COMPAQ DESKPRO EP|COMPAQ EVO D500|COMPAQ EVO D510|COMPAQ EVO D530|COMPAQ EVO D530 CMT|COMPAQ EVO W6000|COMPAQ EVO W8000|COMPAQ PROLIANT HD|COMPAQ PROLIANT ML330|COMPAQ PROLIANT ML380|COMPAQ PROSIGNIA|COMPAQ SP 750|COMPAQ XW6000|DELL OPTIPLEX|HP DC7100|IBM PC 300GL|IBM PC 300PL|UNISYS TELLER STATION|"
$results = StringRegExp($data, '\|([cC].*?)\|', 3)

$listStr = ""
For $i = 0 To UBound($results) - 1
    $listStr &= $results[$i] & "|"
Next
$listStr = StringTrimRight($listStr, 1)

MsgBox(0,"",$listStr)

The pattern looks first for a pipe symbol "|" (yes, it does have to be escaped with "\" because it's a special character). Then it looks for either a lowercase or uppercase c , then 0 or more of any character ("."), and the "?" means to look for the shortest match rather than the largest, so if you left the question mark off, it would match

"|COMPAQ DC5000|COMPAQ DESKPRO 2000|COMPAQ DESKPRO 4000|COMPAQ DESKPRO 5100|COMPAQ DESKPRO 6000|COMPAQ DESKPRO EN|COMPAQ DESKPRO EN SFF|COMPAQ DESKPRO EN-CMT|COMPAQ DESKPRO EP|COMPAQ EVO D500|COMPAQ EVO D510|COMPAQ EVO D530|COMPAQ EVO D530 CMT|COMPAQ EVO W6000|COMPAQ EVO W8000|COMPAQ PROLIANT HD|COMPAQ PROLIANT ML330|COMPAQ PROLIANT ML380|COMPAQ PROSIGNIA|COMPAQ SP 750|COMPAQ XW6000|DELL OPTIPLEX|HP DC7100|IBM PC 300GL|IBM PC 300PL|UNISYS TELLER STATION|"

for its first and only match, which we don't want. I then took it a step further to reformat the matches into the format you need to create your list view (something|something|something).

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Link to comment
Share on other sites

I tested it against the master list I use. It is close, but I'm still getting this in the capture string:

|MORTGAGEWARE.AUTO--ERR|ZDA/DDA

I needed to add to the pattern so it now looks like this:

$pattern = "(" & $start & "[^C][a-zA-Z0-9 :()-_!]*" & $end & ")"
Edited by vollyman
Link to comment
Share on other sites

Tried this also, and I get a blank for a return

Global Const $_StackEmpty = "Empty"
$data= "Compaq test|HP test|Code test|Compaq test2|Compaq test3"
;$arr = StringRegExp("[Compaq]",$data,3)
$ary = StringSplit($data, "|")
$stack = $_StackEmpty
For $i = 1 to $ary[0]
   If StringLeft($ary[$i], 1) = "C" Then
      _StackPush($stack, $ary[$i])
   EndIf
Next
;;;;Now, $stack is your array of values.
MsgBox(0,"",$stack);test to see if desired string is correct
#region _Stack UDFs by nfwu
Func _StackPop(ByRef $avArray)
    Local $sLastVal
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return $_StackEmpty
    EndIf
    $sLastVal = $avArray[UBound($avArray) - 1]
    If UBound($avArray) = 1 Then
        $avArray = $_StackEmpty
    Else
        ReDim $avArray[UBound($avArray) - 1]
    EndIf
    
    Return $sLastVal
EndFunc
Func _StackPush(ByRef $avArray, $sValue)
    IF IsArray( $avArray ) Then
        ReDim $avArray[Ubound($avArray)+1]
    Else
        Dim $avArray[1]
    EndIf
    $avArray[UBound($avArray)-1] = $sValue
    SetError(0)
    Return 1
EndFunc
#endregion
$stack is an array!!!!!!!!!!!!!!!!!!!!!! That's why you get a blank...

try this:

Global Const $_StackEmpty = "Empty"
$data= "Compaq test|HP test|Code test|Compaq test2|Compaq test3"
;$arr = StringRegExp("[Compaq]",$data,3)


$ary = StringSplit($data, "|")
$stack = $_StackEmpty
For $i = 1 to $ary[0]
   If StringLeft($ary[$i], 1) = "C" Then
      _StackPush($stack, $ary[$i])
     ;$stack = $stack & "|" & $ary[$i]
   EndIf
Next
;;;;Now, $stack is your array of values.
For $i = 1 to UBound( $stack ) -1
MsgBox(0,"",$stack[$i]);test to see if desired string is correct
Next
#region _Stack UDFs by nfwu
Func _StackPush(ByRef $avArray, $sValue)
    IF IsArray( $avArray ) Then
        ReDim $avArray[Ubound($avArray)+1]
    Else
        Dim $avArray[1]
    EndIf
    $avArray[UBound($avArray)-1] = $sValue
    SetError(0)
    Return 1
EndFunc
#endregion

#)

Link to comment
Share on other sites

I've been getting quite into RegExp's, and yes, they are very tricky at times.

Here, give this a whirl:

$data = "|APPLE POWERMAC G4|APPLE-IMAC|COMPAQ DC5000|COMPAQ DESKPRO 2000|COMPAQ DESKPRO 4000|COMPAQ DESKPRO 5100|COMPAQ DESKPRO 6000|COMPAQ DESKPRO EN|COMPAQ DESKPRO EN SFF|COMPAQ DESKPRO EN-CMT|COMPAQ DESKPRO EP|COMPAQ EVO D500|COMPAQ EVO D510|COMPAQ EVO D530|COMPAQ EVO D530 CMT|COMPAQ EVO W6000|COMPAQ EVO W8000|COMPAQ PROLIANT HD|COMPAQ PROLIANT ML330|COMPAQ PROLIANT ML380|COMPAQ PROSIGNIA|COMPAQ SP 750|COMPAQ XW6000|DELL OPTIPLEX|HP DC7100|IBM PC 300GL|IBM PC 300PL|UNISYS TELLER STATION|"
$results = StringRegExp($data, '\|([cC].*?)\|', 3)

$listStr = ""
For $i = 0 To UBound($results) - 1
    $listStr &= $results[$i] & "|"
Next
$listStr = StringTrimRight($listStr, 1)

MsgBox(0,"",$listStr)

The pattern looks first for a pipe symbol "|" (yes, it does have to be escaped with "\" because it's a special character). Then it looks for either a lowercase or uppercase c , then 0 or more of any character ("."), and the "?" means to look for the shortest match rather than the largest, so if you left the question mark off, it would match

"|COMPAQ DC5000|COMPAQ DESKPRO 2000|COMPAQ DESKPRO 4000|COMPAQ DESKPRO 5100|COMPAQ DESKPRO 6000|COMPAQ DESKPRO EN|COMPAQ DESKPRO EN SFF|COMPAQ DESKPRO EN-CMT|COMPAQ DESKPRO EP|COMPAQ EVO D500|COMPAQ EVO D510|COMPAQ EVO D530|COMPAQ EVO D530 CMT|COMPAQ EVO W6000|COMPAQ EVO W8000|COMPAQ PROLIANT HD|COMPAQ PROLIANT ML330|COMPAQ PROLIANT ML380|COMPAQ PROSIGNIA|COMPAQ SP 750|COMPAQ XW6000|DELL OPTIPLEX|HP DC7100|IBM PC 300GL|IBM PC 300PL|UNISYS TELLER STATION|"

for its first and only match, which we don't want. I then took it a step further to reformat the matches into the format you need to create your list view (something|something|something).

Tried yours against my master list. I'm getting about a 50%failure rate. Examples of what it missed are:

CA ENDEVOR|CA SPOOL|CA11 JOB RESTART RERUN|CACTUS|
Link to comment
Share on other sites

Would you mind attaching your master list? The reason I'm asking this is because on my computer it catches every one of those you say it doesn't.

$data = "|APPLE POWERMAC G4|APPLE-IMAC|COMPAQ DC5000|COMPAQ DESKPRO 2000|COMPAQ DESKPRO 4000|COMPAQ DESKPRO 5100|COMPAQ DESKPRO 6000|COMPAQ DESKPRO EN|COMPAQ DESKPRO EN SFF|COMPAQ DESKPRO EN-CMT|COMPAQ DESKPRO EP|COMPAQ EVO D500|COMPAQ EVO D510|COMPAQ EVO D530|COMPAQ EVO D530 CMT|COMPAQ EVO W6000|COMPAQ EVO W8000|COMPAQ PROLIANT HD|COMPAQ PROLIANT ML330|COMPAQ PROLIANT ML380|COMPAQ PROSIGNIA|COMPAQ SP 750|COMPAQ XW6000|DELL OPTIPLEX|HP DC7100|IBM PC 300GL|IBM PC 300PL|UNISYS TELLER STATION|CA ENDEVOR|CA SPOOL|CA11 JOB RESTART RERUN|CACTUS|"
$results = StringRegExp($data, '\|([cC].*?)\|', 3)

$listStr = ""
For $i = 0 To UBound($results) - 1
    $listStr &= $results[$i] & "|"
Next
$listStr = StringTrimRight($listStr, 1)

MsgBox(0,"",$listStr)

This works just fine.

Edited by neogia

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Link to comment
Share on other sites

I sent vollyman a PM on this topic, I was not sure that I was understanding the requirements.

Here is a possible solution without RegExp:

$data = "APPLE POWERMAC G4|APPLE-IMAC|COMPAQ DC5000|COMPAQ DESKPRO 2000|COMPAQ DESKPRO 4000|COMPAQ DESKPRO 5100|COMPAQ DESKPRO 6000|COMPAQ DESKPRO EN|COMPAQ DESKPRO EN SFF|COMPAQ DESKPRO EN-CMT|COMPAQ DESKPRO EP|COMPAQ EVO D500|COMPAQ EVO D510|COMPAQ EVO D530|COMPAQ EVO D530 CMT|COMPAQ EVO W6000|COMPAQ EVO W8000|COMPAQ PROLIANT HD|COMPAQ PROLIANT ML330|COMPAQ PROLIANT ML380|COMPAQ PROSIGNIA|COMPAQ SP 750|COMPAQ XW6000|DELL OPTIPLEX|HP DC7100|IBM PC 300GL|IBM PC 300PL|UNISYS TELLER STATION"
$dataARRAY = StringSplit($data, "|")

$match = "C"
$output = ""

For $i = 1 To $dataARRAY[0]
    If $match == StringLeft($dataARRAY[$i], StringLen($match)) Then
       ;ConsoleWrite($dataARRAY[$i] & "|")
        $output = $output & $dataARRAY[$i] & "|"
    EndIf
Next

$output = StringTrimRight($output, 1)

MsgBox(0, "", $output)
You can change the "$match == StringLeft" to "$match = StringLeft" if you want to match C and c.

Using StringLen allows you to search/match "HP" if needed...

vollyman stated in a PM, "I'm going to need to send the string to a ini file."

I'm not sure what format this needs to be in for the INI.

COMPAQ DC5000

COMPAQ DESKPRO 2000

COMPAQ DESKPRO 4000

COMPAQ DESKPRO 5100

COMPAQ DESKPRO 6000

...

or

COMPAQ DC5000|COMPAQ DESKPRO 2000|COMPAQ DESKPRO 4000|COMPAQ DESKPRO 5100|COMPAQ DESKPRO 6000...

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

@herewasplato: Dude, YOU ROCK! Thank You! That works perfectly! I've been beating myself up with this for a few days now.

I needed it in a single string, which this does to a "T". It will allow for me to update the tool that has in the past taken me hours to update by hand, and needs to be done every 2 weeks. :think::(:);)

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