GeorgeLittle
(Fresh Scripter)
2011-07-08 12:37 PM
This should be simple right ?

What I would like to do is the following

Open a text file thats csv delminated with two fields

"Username","UID"

I then want my script to match the KIX @USERNAME variable to the entry in this text file and then load in UID into a variable.

Any pointers greatfully recieved .


Mart
(KiX Supporter)
2011-07-08 01:31 PM
Re: Read CSV file and compare values

Yep, should be easy.
A push into the right direction? Sure, have a look at ReadLine, While – Loop and Split.
A silver platter ready for copy paste without learning anything? Open the spoiler.

Warning, Spoiler:

 Code:
Break on

$rc = Open(1, "c:\somefile.csv", 2)

$line = ReadLine(1)
While @ERROR = 0
	$line = Split($line, ",")
	If $line[0] = @USERID
		$uid = $line[1]
		? "Found name with UID: " + $uid		
	EndIf
	$line = ReadLine(1)
Loop

$rc = Close(1)



Glenn BarnasAdministrator
(KiX Supporter)
2011-07-08 03:58 PM
Re: This should be simple right ?

George,

While Mart's example might work for your data, CSV data can be complicated by the fact that it can contain any text, including quotes, commas, and spaces. These are used by the delimiting process, so data containing spaces or commas MUST be enclosed in quotes, and data containing quotes must be wrapped in multiple quotes.

The CSV() UDF (posted here in UDFs, and the most up-to-date on the Kix UDF Library on my web site) will handle all of these embedded characters. The CSV UDF accepts a single argument - a CSV text string or an array of data, and returns the complementary result - an array or formatted CSV string.

Here's some pseudocode:
 Code:
$aData = FileIO("filespec", "R") ; UDF to load a file into an array
Dim $aUsers[UBound($aData)] ; create an array of user data records
Dim $aUIDs[UBound($aData)] ; create an array of UID records
For $I = 0 to UBound($aData) ; process the input file data array
  $aTmp = CSV($aData[$I]) ; convert CSV string to array
  ; the output array is 2 dimensional - UserName and UID
  $aUsers[$I] = $aTmp[0]
  $aUIDs[$I]  = $aTmp[1]
Next
Now that the CSV data is properly parsed and loaded into an array, you can use AScan to search the Users array. This returns an index where the user ID was found. You use that index to retrieve the corresponding UID from the UID array.

I used 2 arrays to keep the AScan process simple, but you could use a single 2-dimension array to minimize var use.

Glenn