jazzyjeff Posted June 21, 2012 Share Posted June 21, 2012 I am trying to extract information from a CSV file and store the value into another file. This part is fine. Now I am trying to fix an issue where I want to trim the returned value from an array to certain number of characters. For example: I return a value of "Technology". I want the string to be only 4 characters, so I figure I could remove 1 character at a time until the String length is 4 characters. I can't always trim 6 characters though because I don't know if the returned string is always going to be the same length, so this is why I remove 1 character at a time until I reach the desired string length. The problem is that the program never outputs the results for me in a message box like it's supposed, so I think it is stuck in it's loop. Here is the code. $csvLines = InputBox("CSV File","Please enter the number of lines in your CSV file.") $csvDir = FileSelectFolder("Please select the directory of your CSV File.","C:\") For $i = 2 To $csvLines $data = FileReadLine($csvDir & "\data.csv",$i) $dataArray = StringSplit($data,",") If StringLen($dataArray[2]) > 8 Then Do StringTrimRight($dataArray[2],1) Until StringLen($dataArray[2]) = 8 $2 = $dataArray[2] Else $2 = $dataArray[2] EndIf If StringLen($dataArray[3]) > 3 Then Do StringTrimRight($dataArray[3],1) Until StringLen($dataArray[3]) = 3 $3 = $dataArray[3] Else $3 = $dataArray[3] EndIf If StringLen($dataArray[1]) > 2 Then Do StringTrimLeft($dataArray[1],1) Until StringLen($dataArray[1]) = 2 $1 = $dataArray[1] Else $1 = $dataArray[1] EndIf $dataArray = StringSplit($data,",") MsgBox(0,"",$2 & $3 & $1 & @CR & @CR & $dataArray[2] & ", " & $dataArray[3] & ", " & $dataArray[1]) Next If anyone can see how I can resolve this I'd be very appreciative. Thanks. Link to comment Share on other sites More sharing options...
Mechaflash Posted June 21, 2012 Share Posted June 21, 2012 You could use StringLeft($sString, 4) to get the first 4 characters of the string, and create a conditional based on the StringLen() of the item. So if the item is only 4 characters or less, skip performing the StringLeft() Spoiler “Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.” Link to comment Share on other sites More sharing options...
jazzyjeff Posted June 21, 2012 Author Share Posted June 21, 2012 That worked great! Thank you for the help. $csvLines = InputBox("CSV File","Please enter the number of lines in your CSV file.") $csvDir = FileSelectFolder("Please select the directory of your CSV File.","C:\") For $i = 2 To $csvLines $data = FileReadLine($csvDir & "\Data.csv",$i) $dataArray = StringSplit($data,",") If StringLen($dataArray[2]) > 8 Then $2 = StringLeft($dataArray[2],8) Else $2 = $dataArray[2] EndIf If StringLen($dataArray[3]) > 3 Then $3 = StringLeft($dataArray[3],3) Else $3 = $dataArray[3] EndIf If StringLen($dataArray[1]) > 2 Then $1 = StringRight($dataArray[1],2) Else $1 = $dataArray[1] EndIf MsgBox(0,"",$2 & $3 & $1 & @CR & @CR & $dataArray[2] & ", " & $dataArray[3] & ", " & $dataArray[1]) Next Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now