StrQ() : String queue of Recent Items + FileQ() : Validates a File queue

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

StrQ() : String queue of Recent Items + FileQ() : Validates a File queue

09 Oct 2018, 08:33

StrQ() + FileQ() combo is helpful to maintain a "Recent files" menu in a GUI.
StrQ() maintains a pipe delimited (FIFO) queue of (unique) strings

Parameters:
  • List The variable containing queue
    Item A new (unique) string to be added to the top of list. An existing item will be bumped to the top.
    Max String capacity of Queue
    D Item Delimiter. Default is pipe "|".
Function ( w/ usage examples)

Code: Select all

StrQ(Q, I, Max:=10, D:="|") { ;          StrQ v.0.90,  By SKAN on D09F/D34N @ tiny.cc/strq
Local LQ:=StrLen(Q), LI:=StrLen(I), LD:=StrLen(D), F:=0
Return SubStr(Q:=(I)(D)StrReplace(Q,InStr(Q,(I)(D),,0-LQ+LI+LD)?(I)(D):InStr(Q,(D)(I),0,LQ
-LI)?(D)(I):InStr(Q,(D)(I)(D),0)?(D)(I):"","",,1),1,(F:=InStr(Q,D,0,1,Max))?F-1:StrLen(Q))
}

; Usage examples:


#SingleInstance, Force

MAXQ := 10

Que := ""
Loop 9
 Que := StrQ( Que, "Item" A_Index, MAXQ, "`n" )
MsgBox % Que

Que := StrQ( Que, "Item10", MAXQ, "`n" ) ; add a new item to Que 
MsgBox % Que

Que := StrQ( Que, "Item11", MAXQ, "`n" ) ; add a new item to Que.. oldest item "Item1" is dequeued  
MsgBox % Que

Que := StrQ( Que, "Item5", MAXQ, "`n" ) ; Bump up an existing item to top of Que 
MsgBox % Que

MAXQ := 5                               ; Reduce capacity of Queue
Que := StrQ( Que, "`n",    MAXQ, "`n" ) ; Apply the changes, Note: Parameter 2 should be the delimiter 
MsgBox % Que

Latest := StrQ( Que,"`n", 1, "`n" )     ; Retrieve the "top-most = latest = first" item in queue 
MsgBox % Latest

GUI based example:
  • Image

Code: Select all

#NoEnv
#SingleInstance, Force

MAXQ := 14
Fruits := "Apple|Banana|Cherry|Durian|Fig|Grape|Kumquat|Lychee|Mango|Nectarine|Orange|Papaya|Quince|Yuzu"

Gui, Font, S11, Calibri
Gui, Margin, 5, 5
Gui, Add, Text, xm ym w400, Type a string and press <Enter> 
Gui, Add, ComboBox, xm y+5 wp r14 Simple vChoice, %Fruits%
Gui, Add, Button, xm+10 y+10 w100 h24 Default gSelFruit, Add/Select
Gui, Show,,StrQ() demo
Return

GuiClose:
 ExitApp
 
SelFruit:
 GuiControlGet, Choice
 StringReplace, Choice, Choice, |,, All                        ; Delimiter shouldn't be present in string 
 Fruits := StrQ( Fruits, Choice, MAXQ )
 GuiControl,, Choice, % "|" Fruits 
Return

FileQ() Removes non-existent file paths from a queue

Parameters:
  • List The variable containing queue
    Folder Optional. A common prefix like "F:" or "D:\My folder". Depends on how you queued file paths.
    D Item Delimiter. Default (and suggested) is pipe "|".
Pipe is convenient when storing a queue in .INI file

Function ( w/ usage examples)

Code: Select all

FileQ( List, Folder := "", D := "|" ) {  ; by SKAN | 01-Oct-2018 | Topic: goo.gl/R4QrF4
Local NewList := "", F := ( Folder<> "" ? Folder "\" : "" ) 
  Loop, Parse, List, %D%
    If FileExist( F A_LoopField )
       NewList .= A_LoopField D 
Return RTrim( NewList,D )
}

; Usage example

#NoEnv
#SingleInstance, Force

FileList := "asd.txt|fgh.ahk|Installer.ahk|license.txt|AutoHotkey.chm|qwerty"
SplitPath, A_AhkPath,, Folder
MsgBox % FileQ( FileList, Folder )
My Scripts and Functions: V1  V2
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: StrQ() : String queue of Recent Items + FileQ() : Validates a File queue

09 Oct 2018, 09:34

i've had to hack this myself before, so its nice to have a succinct function.
i think this type of function would be a perfect candidate for unit tests

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: StrQ() : String queue of Recent Items + FileQ() : Validates a File queue

10 Oct 2018, 04:15

Thank you. :)

StrQ() is a rewrite of InCache() to make use of the newer StrReplace() and Trim().
My Scripts and Functions: V1  V2
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: StrQ() : String queue of Recent Items + FileQ() : Validates a File queue

10 Oct 2018, 07:07

Nice SKAN I am currently figuring this sort of things out too so Ill study your code. I am doing this with iniread/write to make ini able to append values in front of existing keys.

Thanks for sharing that :)
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

StrQ() : code updated

13 May 2020, 11:49

StrQ() rewritten.

Old version for ref:

Code: Select all

StrQ( List, Item, Max:=10, D:="|", F:=0  ) { ; by SKAN | 15-Sep-2017 | Topic: goo.gl/R4QrF4
Return ( StrLen(Item) ? (Max := InStr( (List := Trim(Item D Trim( StrReplace((List := D List D)
, (F := InStr(List,D Item D)) ? D Item D : "", D),D),D)),D,,,Max)) ? SubStr(List,1,Max-1) : List : List)
}
My Scripts and Functions: V1  V2

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 81 guests