Page 1 of 1 1
Topic Options
#202607 - 2011-07-08 12:37 PM This should be simple right ?
GeorgeLittle Offline
Fresh Scripter

Registered: 2011-02-08
Posts: 47
Loc: UK
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 .

Top
#202608 - 2011-07-08 01:31 PM Re: Read CSV file and compare values [Re: GeorgeLittle]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
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)



Edited by Mart (2011-07-08 01:34 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#202610 - 2011-07-08 03:58 PM Re: This should be simple right ? [Re: GeorgeLittle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.056 seconds in which 0.023 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org