AutoHotkey Community

It is currently May 27th, 2012, 7:13 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: SetFormat, alpha ...
PostPosted: September 30th, 2004, 9:14 pm 
Offline

Joined: April 15th, 2004, 5:33 pm
Posts: 181
I've to create an order file. Its format is specified, means each field of a data record has a fixed length.
All fields are right-justified. A field can contain numeric or alpha chars.
If numeric chars are used the field has to be padded with zeros. Thats no problem with AHK.

Quote:
Var = 12
SetFormat, float, 06.0
Var += 0 ; Sets Var to be 000012


But with literals there's no such option

Quote:
Var = ABCDEFGH
SetFormat, alpha, 30
Var += 0 ; Sets Var to be a 30 char string padded with spaces


Currently it needs some math :cry: and a Loop

Code:
Alphas = ABCDEFGH
FieldLen = 30
StringLen, NumAlphas, Alphas
FieldLen -= %NumAlphas%

Loop, %FieldLen%
     {
     FileAppend, %A_Space%, C:\MyFile.txt
     }
FileAppend, %Alphas%, C:\MyFile.txt


As there's a slight chance that I've overseen something in the helpfile, I've made that request here - instead on the wish list.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 1st, 2004, 2:33 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
I think there is a slightly easier way to do it:
Code:
30Spaces = x                              x
StringReplace, 30Spaces, 30Spaces, x,, All
Alphas = ABCDEFGH
AutoTrim, off
PaddedString = %30Spaces%%Alphas%
StringRight, PaddedString, PaddedString, 30
MsgBox, "%PaddedString%"


You could also do the above with a loop that appends spaces to the beginning of the string based on its initial length. Both of these approaches would probably improve performance and reduce disk activity compared to the one you posted.

Because the need to add padding is somewhat rare, and because there are workarounds, I haven't added this to the wish list yet. If anyone else feels this is a common scripting requirement, feel free to add your vote.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 1st, 2004, 10:18 am 
Offline

Joined: April 15th, 2004, 5:33 pm
Posts: 181
Thx for your support Chris.

Beasty


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], Leef_me, sjc1000 and 77 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group