AutoHotkey Community

It is currently May 27th, 2012, 11:31 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: September 30th, 2009, 8:42 pm 
How to get random 6 items or random 4 items or random anything from a list?


suppose i have a long list
aa
bbbbb
dddd
eeee
tadd
egg
mad
canibal
ddd
rteter
egglo
hhhhhhr
rtrrtrtr
..............


A function(4,"list.txt) that could get four random items from the list or
a function(e,"list.txt") that would get all the items starting with "e"
and if its function(egg,"list.txt) would get all the items starting with the string "egg"


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 9:14 pm 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6074
Location: San Diego, California
Here are several commands related to YOUR function/project

http://www.autohotkey.com/docs/commands/Random.htm
http://www.autohotkey.com/docs/commands/Sort.htm
http://www.autohotkey.com/docs/commands/LoopParse.htm
http://www.autohotkey.com/docs/commands/Loop.htm

Depending on where you store your list, you might need another Loop??? or a File??? command. Here is the full list of commands

http://www.autohotkey.com/docs/commands.htm

If you can come up with something but have troubles, post your code and those in the forum can probably help.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 9:24 pm 
here is some code u can play with
Code:
list =
(
aa
bbbbb
dddd
eeee
tadd
egg
mad
canibal
ddd
rteter
egglo
hhhhhhr
rtrrtrtr
)

function(4, list)
return

function(var, list)
{
StringSplit, ListArray, list, `n`r
Loop
{
random, selected, 1, %ListArray0%
check := ListArray%selected%
if check !=
{
show = %show%`n%check%
ListArray%selected% =
x++
}
if (x = var)
break
}

msgbox, %show%
}


for ur second and third require, try to add StringLeft with some if-else commands


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 9:54 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
Here's a version I came up with:

Code:
var=
(
aa
bbbbb
dddd
eeee
tadd
egg
mad
canibal
ddd
rteter
egglo
hhhhhhr
rtrrtrtr
)

!j::MsgBox % rItems(6,var)

/*
no - the number of items to retrieve from the list
input - the list variable to retrieve the items from
*/

rItems(no,input) {

  VarSetCapacity(res,0)

  Sort, input, Random
  Loop, Parse, input, `n, `r
  {
    if (A_Index > no)
      break
    else
      res .= A_LoopField "`n"
  }
  return res,VarSetCapacity(input,0)

}

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 10:29 pm 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Ok, I've done it.
Get X random items or all items started by a substring:
Code:
varList =
(
aa
bbbbb
dddd
egggyy
eeee
tadd
egg
mad
canibal
ddd
rteter
egglo
hhhhhhr
rtrrtrtr
)

MsgBox % "4: " GetFromList(4,varList)
MsgBox % "e: " GetFromList("e",varList)
MsgBox % "6: " GetFromList(6,varList)
MsgBox % "d: " GetFromList("d",varList)
return

;-----------------------------------------------

GetFromList(what,list,din="`n`r",dout=", ",remdup=0,csen=0)
; what = number or substring
; list = input list
; din = delimiter (input)
; dout = delimiter (output)
; remdup = remove duplicates?
; csen = case sensitive?
{
  remdup := ( remdup ? "U" : "" )
  If what is number
  {
    Sort, list, D%din% Random %remdup%
    Loop, Parse, list, %din%, %din%
    {
      res .= ( res<>"" ? dout : "" ) A_LoopField
      If ( A_Index = what )
        break
    }
  }
  Else
  {
    Sort, list, % "D" din " " remdup ( csen ? " C" : "" )
    Loop, Parse, list, %din%, %din%
      If ( InStr(A_LoopField,what,csen) = 1 )
      {
        found := 1
        res .= ( res<>"" ? dout : "" ) A_LoopField
      }
      Else
        If found
          break
  }
  return res
}

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 10:30 pm 
save me time, thanks for the code
bye


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2009, 11:19 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
Here's a slightly more thought out version:

Code:
;var=aa,bbb,cccc,dd,eee,ffff,gg,hhh,iiii,jj,kkk,llll
var=
(
aa
bbbbb
dddd
eeee
tadd
egg
mad
canibal
ddd
rteter
egglo
hhhhhhr
rtrrtrtr
)

;!j::MsgBox % rItems(4,var,"`,")
!j::MsgBox % rItems(4,var,"`n","`r")
/*
no - number of items to retrieve
input - list variable with the items to be retrieved
delim - item delimiter
omit - omit character (optional)
*/
rItems(no,input,delim,omit="") {

  VarSetCapacity(res,0) ; sets res to blank to check parameters

  StringReplace, max, input, % delim, , UseErrorLevel ; check no. of items by delimiter
  if (ErrorLevel < no)
    return res,VarSetCapacity(max,0) ; if 'no' exceeds ErrorLevel returns nothing, out of bounds
  Sort, input, Random D%delim% ; sorts input randomly by delimiter
  Loop, Parse, input, % delim, % omit
  {
    if (A_Index > no)
      break ; breaks loop once A_Index exceeds no. of items
    else
      res .= A_LoopField "`n"
  }
  return res,VarSetCapacity(max,0)

}

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


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: Bing [Bot], Google Feedfetcher, Yahoo [Bot] and 21 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