AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SetFormat, alpha ...

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Beastmaster



Joined: 15 Apr 2004
Posts: 181

PostPosted: Thu Sep 30, 2004 8:14 pm    Post subject: SetFormat, alpha ... Reply with quote

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 Crying or Very sad 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.
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10480

PostPosted: Fri Oct 01, 2004 1:33 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
Beastmaster



Joined: 15 Apr 2004
Posts: 181

PostPosted: Fri Oct 01, 2004 9:18 am    Post subject: Reply with quote

Thx for your support Chris.

Beasty
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group