AutoHotkey Community

It is currently May 26th, 2012, 3:22 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: March 10th, 2009, 7:21 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2009, 11:08 am 
So non of the 'Sort' parameters worked for you?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2009, 12:20 pm 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
:D

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
}

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 10th, 2009, 6:44 pm 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 10th, 2009, 7:06 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2009, 7:08 pm 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
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. :lol:

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2009, 2:11 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
Thanks. My being right brain dominant has its advantages but understanding ahk ain't one of them. Thanx again!

_________________
...Ed


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, nyoe, patgenn123, Yahoo [Bot] and 17 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