Jump to content

if string contains to array


Recommended Posts

i have a text file with a list of initials. i want to search a csv file for these initials.

1. i would like it to return the lines that contain these initials.

2. i would like to return all instances of values that start with "L00" OR "D00"

so far i have #1 done, but am having some trouble getting #2.

here's what i have so far:

$afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4 )

If @error Then
    MsgBox(262144,"Find Assets","No file was chosen")
EndIf

Dim $initials
If Not _FileReadToArray($afile,$initials) Then
 Exit
EndIf

For $x = 1 to $initials[0]


$filename="lao_assets.csv"

$line_text_input = $initials[$x]
$file_count_lines = _FileCountLines($filename)
for $i = 0 to $file_count_lines
    $Lines_text_output = FileReadLine($filename, $i)
    if StringInStr($Lines_text_output, $line_text_input) then
        msgbox(0, "", $Lines_text_output)
        $success = True
        
        
    EndIf
Next
Next
Link to comment
Share on other sites

I just want to make sure I understand what you're doing versus what you're trying to do.

1. You read a file with initials and put all that data into an array.

2. You look through the .csv file for each instance of each initial you have a message box pop up, but it is only returning the first value found.

But you'd also like to have each instance of just the "LOO" OR "DOO" and nothing else.

Also I cleaned up your code a bit, since you only need to create the filename once not every single time you go through a loop, you don't need to recalculate the line count every time you loop, and FileReadLine returns the first line beginning with 1 not 0.

Dim $initials

$afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4 )

If @error Then
    MsgBox(262144,"Find Assets","No file was chosen")
EndIf


If Not _FileReadToArray($afile,$initials) Then Exit

$filename="lao_assets.csv"
$file_count_lines = _FileCountLines($filename)

For $x = 1 to $initials[0]
    $line_text_input = $initials[$x]    
    
    ConsoleWrite("file_count_lines: " & $file_count_lines & @CR)
    for $i = 1 to $file_count_lines
        $Lines_text_output = FileReadLine($filename, $i)
        if StringInStr($Lines_text_output, $line_text_input) then
            msgbox(0, "", $Lines_text_output)
            $success = True         
        EndIf
    Next
Next
Link to comment
Share on other sites

yep thats correct... "L00" or "D00" are just the beginning of the string tho. (essentially, its the computer names of peoples computers).

thanks!

I just want to make sure I understand what you're doing versus what you're trying to do.

1. You read a file with initials and put all that data into an array.

2. You look through the .csv file for each instance of each initial you have a message box pop up, but it is only returning the first value found.

But you'd also like to have each instance of just the "LOO" OR "DOO" and nothing else.

Also I cleaned up your code a bit, since you only need to create the filename once not every single time you go through a loop, you don't need to recalculate the line count every time you loop, and FileReadLine returns the first line beginning with 1 not 0.

Dim $initials

$afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4 )

If @error Then
    MsgBox(262144,"Find Assets","No file was chosen")
EndIf


If Not _FileReadToArray($afile,$initials) Then Exit

$filename="lao_assets.csv"
$file_count_lines = _FileCountLines($filename)

For $x = 1 to $initials[0]
    $line_text_input = $initials[$x]    
    
    ConsoleWrite("file_count_lines: " & $file_count_lines & @CR)
    for $i = 1 to $file_count_lines
        $Lines_text_output = FileReadLine($filename, $i)
        if StringInStr($Lines_text_output, $line_text_input) then
            msgbox(0, "", $Lines_text_output)
            $success = True         
        EndIf
    Next
Next
Link to comment
Share on other sites

Ok, so you want anything that has L00* or D00* till the next comma? is what I'm thinking, in that case I'd use a Stringsplit. Do you want the L00 or D00 of only those people who's initials you've found or every single one, I have written code for both just see which one suits you best.

The LDFound array is a multi dimensional array where LDFound[0][0] has the count of how many lines were found, and LDFound[a number][0] has the line of text and LDFound[a number][1] is the line number.

Code of only those people who've initials have been found L00 and D00:

Dim $initials

$afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4 )

If @error Then
    MsgBox(262144,"Find Assets","No file was chosen")
EndIf

If Not _FileReadToArray($afile,$initials) Then Exit

$filename="lao_assets.csv"
$file_count_lines = _FileCountLines($filename)
Dim $LDFound[$file_count_lines][1]

$count = 0

For $x = 1 to $initials[0]
    $line_text_input = $initials[$x]    
    for $i = 1 to $file_count_lines
        $Lines_text_output = FileReadLine($filename, $i)        
        if StringInStr($Lines_text_output, $line_text_input) then
            $temp = StringSplit($Lines_text_output, ",")
            msgbox(0, "", $Lines_text_output)
            $success = True
            If StringRegExp($temp[1], "(?i)(L00)(.*)") Then
                $LDFound[$count][0] = $Lines_text_output
                $LDFound[$count][1] = $i
                $count += 1
                $LDFound[0][0] = $count
            ElseIf StringRegExp($temp[1], "(?i)(D00)(.*)") Then
                $LDFound[$count][0] = $Lines_text_output
                $LDFound[$count][1] = $i
                $count += 1
                $LDFound[0][0] = $count
            EndIf                       
        EndIf
    Next
Next

And code if you just want every line that has L00 or D00

Dim $initials

$afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4 )

If @error Then
    MsgBox(262144,"Find Assets","No file was chosen")
EndIf

If Not _FileReadToArray($afile,$initials) Then Exit

$filename="lao_assets.csv"
$file_count_lines = _FileCountLines($filename)
Dim $LDFound[$file_count_lines][1]

$count = 0

For $x = 1 to $initials[0]
    $line_text_input = $initials[$x]    
    for $i = 1 to $file_count_lines
        $Lines_text_output = FileReadLine($filename, $i)        
        if StringInStr($Lines_text_output, $line_text_input) then
            $temp = StringSplit($Lines_text_output, ",")
            msgbox(0, "", $Lines_text_output)
            $success = True             
        EndIf
    Next
Next

For $i = 1 To $file_count_lines
    $Lines_text_output = FileReadLine($filename, $i)
    $temp = StringSplit($Lines_text_output, ",")
    If StringRegExp($temp[1], "(?i)(L00)(.*)") Then
        $LDFound[$count][0] = $Lines_text_output
        $LDFound[$count][1] = $i
        $count += 1
        $LDFound[0][0] = $count     
    ElseIf StringRegExp($temp[1], "(?i)(D00)(.*)") Then
        $LDFound[$count][0] = $Lines_text_output
        $LDFound[$count][1] = $i
        $count += 1
        $LDFound[0][0] = $count
    EndIf                       
Next
Edited by cartman380
Link to comment
Share on other sites

great!

id like to isolate the L00 and/or the D00 though.

essentially im trying to identify the computers that belong to a list of users..

theres so much more info on each line info that i dont need

so end result..

<initials> has l00384834

<initials> has d0093949

etc

thanks cartman!

Link to comment
Share on other sites

Ok, is the .csv file actually seperated by commas? because if it is you can do that easy enough. This should do the trick.

Dim $initials

$afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4 )

If @error Then
    MsgBox(262144,"Find Assets","No file was chosen")
EndIf

If Not _FileReadToArray($afile,$initials) Then Exit

$filename="lao_assets.csv"
$file_count_lines = _FileCountLines($filename)
Dim $LDFound[$file_count_lines][1]

$count = 1

For $x = 1 to $initials[0]
    $line_text_input = $initials[$x]    
    for $i = 1 to $file_count_lines
        $Lines_text_output = FileReadLine($filename, $i)        
        if StringInStr($Lines_text_output, $line_text_input) then
            $temp = StringSplit($Lines_text_output, ",")
            msgbox(0, "", $Lines_text_output)
            $success = True
            For $j = 0 to $temp[0] - 1
                If StringRegExp($temp[$j], "(?i)(L00)(\d*)") Then
                    $LDFound[$count][0] = $initials[$x]
                    $LDFound[$count][1] = $temp[$j]
                    $count += 1
                    $LDFound[0][0] = $count - 1
                    ExitLoop
                 ElseIf StringRegExp($temp[1], "(?i)(D00)(.*)") Then
                    $LDFound[$count][0] = $initials[$x]
                    $LDFound[$count][1] = $temp[$j]
                    $count += 1
                    $LDFound[0][0] = $count - 1
                    ExitLoop
                EndIf               
            Next       
        EndIf
    Next
Next

Edited by cartman380
Link to comment
Share on other sites

the csv file has multiple columns and the value i get from the msgbox has commas which indicate the column seperation. the last code you sent me gave me an "array variable has incorrect number of subscripts" error.

ultimately id like a new csv that says which person (initials), has which computers

even in cases where a person has more than one computer

like ABC has l00349 and d009232

hope im making sense =)

again thanks for your help!

Ok, is the .csv file actually seperated by commas? because if it is you can do that easy enough. This should do the trick.

Dim $initials

$afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4 )

If @error Then
    MsgBox(262144,"Find Assets","No file was chosen")
EndIf

If Not _FileReadToArray($afile,$initials) Then Exit

$filename="lao_assets.csv"
$file_count_lines = _FileCountLines($filename)
Dim $LDFound[$file_count_lines][1]

$count = 1

For $x = 1 to $initials[0]
    $line_text_input = $initials[$x]    
    for $i = 1 to $file_count_lines
        $Lines_text_output = FileReadLine($filename, $i)        
        if StringInStr($Lines_text_output, $line_text_input) then
            $temp = StringSplit($Lines_text_output, ",")
            msgbox(0, "", $Lines_text_output)
            $success = True
            For $j = 0 to $temp[0] - 1
                If StringRegExp($temp[$j], "(?i)(L00)(\d*)") Then
                    $LDFound[$count][0] = $initials[$x]
                    $LDFound[$count][1] = $temp[$j]
                    $count += 1
                    $LDFound[0][0] = $count - 1
                    ExitLoop
                 ElseIf StringRegExp($temp[1], "(?i)(D00)(.*)") Then
                    $LDFound[$count][0] = $initials[$x]
                    $LDFound[$count][1] = $temp[$j]
                    $count += 1
                    $LDFound[0][0] = $count - 1
                    ExitLoop
                EndIf               
            Next       
        EndIf
    Next
Next
Link to comment
Share on other sites

My mistake :)

This will work

Dim $initials

$afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4 )

If @error Then
    MsgBox(262144,"Find Assets","No file was chosen")
EndIf

If Not _FileReadToArray($afile,$initials) Then Exit

$filename="lao_assets.csv"
$file_count_lines = _FileCountLines($filename)
Dim $LDFound[$file_count_lines][2]

$count = 1

For $x = 1 to $initials[0]
    $line_text_input = $initials[$x]    
    for $i = 1 to $file_count_lines
        $Lines_text_output = FileReadLine($filename, $i)        
        if StringInStr($Lines_text_output, $line_text_input) then
            $temp = StringSplit($Lines_text_output, ",")
            msgbox(0, "", $Lines_text_output)
            $success = True
            For $j = 0 to $temp[0] - 1
                If StringRegExp($temp[$j], "(?i)(L00)(.*)") Then
                    $LDFound[$count][0] = $initials[$x]
                    $LDFound[$count][1] = $temp[$j]
                    $count += 1
                    $LDFound[0][0] = $count - 1
                    ExitLoop
                 ElseIf StringRegExp($temp[1], "(?i)(D00)(.*)") Then
                    $LDFound[$count][0] = $initials[$x]
                    $LDFound[$count][1] = $temp[$j]
                    $count += 1
                    $LDFound[0][0] = $count - 1
                    ExitLoop
                EndIf               
            Next       
        EndIf
    Next
Next
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...