Page 1 of 1 1
Topic Options
#204458 - 2012-03-14 04:25 PM Count first 8 Character of username
Playford Offline
Fresh Scripter

Registered: 2012-03-14
Posts: 7
Loc: Scotland
Hi all,

This is my first post here.

I use printers that only have space for 8 character for the hold print function.

I need to have a script to get the current username and only use the first 8.

I've only really used KIX to add a couple of print Q's and connect a couple of network drives so my knowledge is quite limited.

From what I've found I need to read the username into an array and use the split command but I'm not sure how to count the 8 characters. Help would be greatly appreciated.

Thanks
Mark

Top
#204459 - 2012-03-14 04:35 PM Re: Count first 8 Character of username [Re: Playford]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Hi Mark,

Welcome to the board.

There is no need to put the userid in an array and use the split command. Below are two examples that get the first 8 characters of the username if the username is longer than 8 characters. If the username is 8 or shorter it takes the full username.

 Code:
Break on

If Len(@USERID) > 8
	$username = Left(@USERID, 8)
Else
	$username = @USERID
EndIf

? "Original username: " + @USERID
? "First 8 of username: " + $username


or

 Code:
If Len(@USERID) > 8
	$username = SubStr(@USERID, 1, 8)
Else
	$username = @USERID
EndIf

? "Original username: " + @USERID
? "First 8 of username: " + $username
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204460 - 2012-03-14 06:09 PM Re: Count first 8 Character of username [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Welcome to KORG, Mark!

Actually
 Code:
$UserName = Left(@USERID, 8)
is all you need. Left() will not return an error if you request more than the number of characters in the string. It will simply return the entire string.

If you use/allow spaces in usernames in your environment, you might need to replace them to make your other task work properly. Not everything works with spaces.

Something like
 Code:
$UserName = Join(Split(Left(@USERID, 8), ' '), '_')
This takes the first 8 chars of the username and replaces any spaces with underscores. If the ID is less than 8, then the entire userID is used (and spaces replaced).

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#204461 - 2012-03-14 08:20 PM Re: Count first 8 Character of username [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
 Originally Posted By: Glenn Barnas

...
Left() will not return an error if you request more than the number of characters in the string. It will simply return the entire string.
...


LOL learned something today. Didn't know and didn't test so I had it coming

I almost always write elaborate scripts just to be sure that others that may not share my logic understand it too and so I able to understand what is going on when opening a script that has been in use as it was originally made months or longer ago. That’s one of the reasons I never participated in a kixgolf challenge. No way for me to get close to the guys that write that freaking short and unreadable stuff and still understand what is going on.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204462 - 2012-03-14 08:41 PM Re: Count first 8 Character of username [Re: Mart]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
 Quote:
That’s one of the reasons I never participated in a kixgolf challenge.


...but see that's where you learn this stuff. Believe me Mart... I used to think the same thing, BUT, there really is value in golfing. I think it makes you a better scripter (but it doesn't help anyone else when trying to read it ;\) )

Top
#204463 - 2012-03-14 11:02 PM Re: Count first 8 Character of username [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
The cats were around again and I figured it was time to skin one a different way was all... ;\)

No right or wrong, simply different. Mart's way clearly illustrates the logic and requires but a "use first 8 chars or all chars if <8" comment to be understood by anyone. My "golfed" example should have a few more comments if used in production to make sure the process is clear.

I comment my code heavily because I often can't remember why I coded something a certain way 3 hours after I coded it. I actually scanned several of my production code projects a while back with a modified version of Sanity and found that my Comment to Code ratio was 1.6:1 D)

Glenn

PS - Golf tournaments are great ways to learn.. and several have found their way (through adaptation) into production scripts, such as the LuhnMod and MOAN network subnet UDFs.
_________________________
Actually I am a Rocket Scientist! \:D

Top
#204468 - 2012-03-15 01:24 PM Re: Count first 8 Character of username [Re: Mart]
Playford Offline
Fresh Scripter

Registered: 2012-03-14
Posts: 7
Loc: Scotland
Thanks so much for the reply. It works perfectly.

All I need to do is now stip out the "." in between the first and last name.

Thanks
Mark

Top
#204469 - 2012-03-15 02:08 PM Re: Count first 8 Character of username [Re: Playford]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
 Code:
$username = Replace(@userid,".","")
$username = Replace($username,"-","")
$username = Replace($username,"_","")
$username = Replace($username," ","")
$username = SubStr($username,1,8)
? $username

Top
#204470 - 2012-03-15 03:00 PM Re: Count first 8 Character of username [Re: Playford]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
If you are removing the character (Arend's example) rather than replacing it (my earlier example) be sure to perform the character removals BEFORE you take the first 8 chars. Arend's example does this, but it's important to notice. Here's a consolidated method that uses an array of chars to remove:
 Code:
$Username=@USERID
$aReplace = ' ','.','_'   ; list of chars in user name to remove
For Each $C in $aReplace
  $Username = Replace($Username, $C, '')
Next
$Username = Left($Username, 8)
Glenn

_________________________
Actually I am a Rocket Scientist! \:D

Top
#204511 - 2012-03-19 11:27 AM Re: Count first 8 Character of username [Re: Glenn Barnas]
Playford Offline
Fresh Scripter

Registered: 2012-03-14
Posts: 7
Loc: Scotland
I would like to say a massive thank you all for your help.

I now have the scipt working perfectly.

Thanks
Mark

Top
#204512 - 2012-03-19 11:32 AM Re: Count first 8 Character of username [Re: Playford]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Nice Loopwork there Glenn \:\)
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
1 registered (mole) and 494 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.064 seconds in which 0.025 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