 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
EdScriptNewbie
Joined: 20 Jan 2007 Posts: 110
|
Posted: Tue Mar 10, 2009 6:21 am Post subject: List: number{tab}letter. Sort first by number then by letter |
|
|
Hi,
My script generates a list:
13{tab}Dog
278{tab}Cat
13{tab}Zebra
3{tab}Lemur
55{tab}Cobra
13{tab}Insect
I want a script to sort it into:
3{tab}Lemur
13{tab}Dog
13{tab}Insect
13{tab}Zebra
55{tab}Cobra
278{tab}Cat
Ie, I want a numeric sort of what's west of the Tab, then an alpha sort within any group of identically numbered lines.
My version just sorts it numerically, but not within the groups of identical numbers.
Thanks
| Code: | fileread,textvar,text.txt
StringLower textvar, textvar
textvar := RegExReplace(textvar, "[\s\W]+", "`n")
Sort textvar
prevWord =
Loop Parse, textvar, `n
{
If (A_LoopField = prevWord)
{
count++
}
Else
{
If (prevWord != "")
{
result .= (count + 1) . "`t" . prevWord . "`n"
}
count := 0
prevWord := A_LoopField
}
}
sort, result, n r
filedelete,wordlist.txt
fileappend,%result%,wordlist.txt
run,wordlist.txt
exitapp
|
_________________ ...Ed |
|
| Back to top |
|
 |
BoBoł Guest
|
Posted: Tue Mar 10, 2009 10:08 am Post subject: |
|
|
| So non of the 'Sort' parameters worked for you? |
|
| Back to top |
|
 |
Micahs
Joined: 01 Dec 2006 Posts: 460
|
Posted: Tue Mar 10, 2009 11:20 am Post subject: |
|
|
| Code: | txt=
(Join
13`tC`n
278`tDog`n
13`tB`n
90`tA`n
3`tCobra`n
54`tAlbatross`n
90`tB`n
13`tA`n
511`tC`n
238`tCat`n
90`tC`n
511`tB`n
1`tStoat`n
55`tPanda`n
511`tA`n
)
MsgBox, % WeirdAlphaNumericalizer(txt)
Return
WeirdAlphaNumericalizer(txt)
{
sort, txt, n ;sorts numerically first - the following loop depends on the numbers being grouped together
pos=1
StringLen, lentxt, txt
Loop, % lentxt
{ If(lineEnd := InStr(txt, "`n", 0, pos)) ;get the current line
{ StringMid, currLine, txt, pos, lineEnd-pos
pos := lineEnd+1
If(lineEnd := InStr(txt, "`n", 0, pos)) ;get the next line
{ StringMid, nextLine, txt, pos, lineEnd-pos
RegExMatch(nextLine, "m)^[\d]+?(?=`t)", nextnum) ;get just the next number
}
}
If(currLine)
{ RegExMatch(currLine, "m)^[\d]+?(?=`t)", num) ;get just the number
If(num=prevNum and num!=nextNum) ;if the last one in the group
{ StringSplit, tmpThing, currLine, %A_Tab%
tmpList .= tmpThing2 . "`t" . tmpThing1 . "`n"
If(tmpList) ;if this list exists, it has several switched items.
{ Sort, tmpList ;Sort them alphabetically
newtmpList=
Loop, Parse, tmpList, `n ;put 'em back
{ StringSplit, tmpThing, A_LoopField, %A_Tab%
If(tmpThing0)
{ newtmpList .= tmpThing2 . "`t" . tmpThing1 . "`n"
}
}
newtxt .= newtmpList ;Add sorted list to new list
tmpList= ;clear it
}
}
Else If(num=prevNum and num=nextNum) or (num!=prevNum and num=nextNum) ;if same as next or last one, switch word and number and add to temp list
{ StringSplit, tmpThing, currLine, %A_Tab%
tmpList .= tmpThing2 . "`t" . tmpThing1 . "`n"
}
Else ;if not same as next or prev one, add to final list
{ newtxt .= currLine . "`n" ;just add the curr item
}
prevNum := num
currLine= ;clear
prevLine=
nextNum=
}
}
Return newtxt
}
|
_________________
 |
|
| Back to top |
|
 |
EdScriptNewbie
Joined: 20 Jan 2007 Posts: 110
|
Posted: Tue Mar 10, 2009 5:44 pm Post subject: Thanks. now for the questions that reveal my ahk limitations |
|
|
It works! I seem to be able to figure out much of what you did (though I'm not sure I could duplicate it from scratch). But, you've used some syntax unfamiliar to me, and I don't see a way to search Autohotkey Help to figure it out, so here goes:
MsgBox, % WeirdAlphaNumericalizer(txt)
Return
Obviously % makes the result of WeirdAlphaNumericalizer(txt) into the text of a message box. What's that use of % called, so I can look it up?
Also, I know how to use subroutines, but I thought they had to end in :. So I'm guessing
WeirdAlphaNumericalizer(txt)
is not a subroutine. True? What is it? The MsgBox % in the command seems to make the thread jump to there, since the MsgBox shows the right answer even though it hasn't yet worked its way down there in the code.
THANKS! _________________ ...Ed |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Tue Mar 10, 2009 6:06 pm Post subject: Re: Thanks. now for the questions that reveal my ahk limitat |
|
|
| EdScriptNewbie wrote: | | What's that use of % called, so I can look it up? | Force an expression
| EdScriptNewbie wrote: | I'm guessing
WeirdAlphaNumericalizer(txt)
is not a subroutine. True? What is it? | Functions |
|
| Back to top |
|
 |
Micahs
Joined: 01 Dec 2006 Posts: 460
|
Posted: Tue Mar 10, 2009 6:08 pm Post subject: |
|
|
I just did that 'cause it's quicker. Sorry. You would probably call it normally like this:
| Code: | result := WeirdAlphaNumericalizer(txt)
MsgBox, %result% ;Or whatever else you need to do with it. |
Only Labels (Subs) end in a ":", this is a function.
The % just tells ahk that what's coming after is an expression, instead of a string or a variable.
[EDIT] What he said. That's what I get for being slow.  _________________
 |
|
| Back to top |
|
 |
EdScriptNewbie
Joined: 20 Jan 2007 Posts: 110
|
Posted: Wed Mar 11, 2009 1:11 am Post subject: |
|
|
Thanks. My being right brain dominant has its advantages but understanding ahk ain't one of them. Thanx again! _________________ ...Ed |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|