Keyword to change number to corresponding letter


(Robin Bilney) #1

Hi,

 

Is there a keyword modifier that could change a number to print the corresponding letter in the alphabet - e.g.  1 = A;  2 = B, 3 = C; etc.?

 

I'm using an asset listing to display points on a static Google Map.  The asset_position keyword is used as the label on the map marker.  However, the Google Maps markers are limited to a single character (0-9).  After 9, the markers display a dot.

 

 

Thanks,

Robin.

 

 

 

 

 

 


(Tim Davison) #2

It's not elegant...

%asset_position^eq:1:A: ^trim%%asset_position^eq:2:B: ^trim%%asset_position^eq:3:C: ^trim%%asset_position^eq:4:D: ^trim%....etc

and keep going.  Not sure what you want to do once it gets over 26.  Perhaps you can do ^modulo:26 first, then it would cycle through the alphabet again.

 

The ' ^trim' modifier is necessary.  The space provides the FALSE option in the conditional (otherwise you get a heap of 0's), and the trim removes the space again (otherwise you end up with a heap of spaces).

 

I'm sure better minds than mine could come up with a brilliant regex or similar.

 

Note: I can't test this at the present so can't guarantee it would work, and I've also had issues using modifiers on the asset_position keyword before (basically they didn't work), but I think that has been fixed in later versions.


(Bart Banda) #3

You could probably use the regex asset for this to make it cleaner in your type format bodycopy. http://manuals.matrix.squizsuite.net/other-cms-assets/chapters/regular-expression-asset

 

Just put your matching and replacement rules in the regex asset and do a simple regex keyword modifier on the asset_position keyword.


(Peter McLeod) #4
Hi
 
How about using unicode characters instead, as A (uppercase) = A and the number part increments to Z for Z (uppercase).
 
For example replace %asset_positon% keyord with:
 
&#%asset_position^add:64%; 
 
The add 64 modifier sets the starting position at uppercase A (A).
 
Potential issues: 
  • Matrix Wysiwyg may modify the the entities to html entities themselves, so you will need to use a raw html container/content type
  • Obviously if you go past 26 assets then other unicode characters will be used. You could probably use some other Matrix keywords to handle this.
See: http://unicode-table.comfor list of all unicode chars
 
Thanks
Peter

(Robin Bilney) #5

Thanks for the help guys.  I went with Bart's suggestion of the regex asset and it's worked a treat. 


(Robin Bilney) #6

Hey all,

 

I've come across a problem with this solution.  I've set up a Regular Expression asset which has the following Regex and Replacement pairs:

 

/1/  -  A

/2/  -  B

/3/  -  C

...

/25/  -  Y

/26/  -  Z

 

On my type format bodycopy I use the following keyword and modifier:

 

%asset_position^preg_replace:[REGEXasset#]%

 

However the problem begins when the position hits double digits.  For 10 onwards it's displaying 'A0', 'AA', 'AB', etc. instead of 'J','K','L', etc.  Regex isn't really my strong point, is there a simple solution to this?

 

Many thanks,

Robin


(Peter McLeod) #7

Hi

You just need to append some constant character to the asset position keyword, and then update the regex asset to match a string that is the number sequence followed by the character.


You could use the append or replace modifiers to add the the character in the listing eg:


%asset_position^append:a^preg_replace:[REGEXasset#]%


Then match the regex on 1a,2a,…,10a etc which are now all unique strings.


If you are on a version that doesn’t support append, use replace to achieve the same effect.


Thanks

Peter


(Robin Bilney) #8

Thanks Peter, that worked for the number 10 but not for 11, 12, etc.  It's now showing

 

/9a/  =  I

/10a/  = J

/11a/  = 1A

/12a/  = 1B

 

I've tried different variations like:  /^11a$/    /(11a)/    /[11a]/   all don't work.  What am I missing?


(Tim Davison) #9

Sorry to hijack, I'm definitely not expert in regex but it looks to me like the confusion is that the first 9 positions will always be a substring of a subsequent position.  In programming vernacular you're doing a string compare rather than a numeric compare.  I'm sure this is not a new problem in regex and cannot believe there is not some way to cater for this, but I'm wondering whether you couldn't do something like:

%asset_position^mod:26^eq:1:A: ^trim%
%asset_position^mod:26^eq:2:B: ^trim%

(Robin Bilney) #10

Thanks Tim, I've gone with your solution in the end and it's working perfectly. I'm definitely not a fan or regex!


(Tim Davison) #11

Oh no, regex is very powerful.  I wish I knew it better.  Tolkien almost had it right: "One regex to rule them all".

 

Glad it's working for you.


(Peter McLeod) #12

Yeah, of course you are right Tim.

With the solution I put forward, if you also prepend with a constant char then it should work.


Eg: a1a, would then not match a11a (where as just adding after would).


Thanks

Petet


(Evan Wills) #13

I did some testing and in some ways the RegEx Bart recommended is very elegant and in others its butt ugly, but it can work. Here's a series of RegExes that will do what you want if you need to go above 26 items:

/^1$/ = A /^2$/ = B
/^3$/ = C
/^4$/ = D
/^5$/ = E
/^6$/ = F
/^7$/ = G
/^8$/ = H
/^9$/ = I
/^10$/ = J
/^11$/ = K
/^12$/ = L
/^13$/ = M
/^14$/ = N
/^15$/ = O
/^16$/ = P
/^17$/ = Q
/^18$/ = R
/^19$/ = S
/^20$/ = T
/^21$/ = U
/^22$/ = V
/^23$/ = W
/^24$/ = X
/^25$/ = Y
/^26$/ = Z
/^27$/ = AA
/^28$/ = AB
/^29$/ = AC
/^30$/ = AD
/^31$/ = AE
/^32$/ = AF
/^33$/ = AG
/^34$/ = AH
/^35$/ = AI
/^36$/ = AJ
/^37$/ = AK
/^38$/ = AL
/^39$/ = AM
/^40$/ = AN
/^41$/ = AO
/^42$/ = AP
/^43$/ = AQ
/^44$/ = AR
/^45$/ = AS
/^46$/ = AT
/^47$/ = AU
/^48$/ = AV
/^49$/ = AW
/^50$/ = AX
/^51$/ = AY
/^52$/ = AZ
/^53$/ = BA
/^54$/ = BB
/^55$/ = BC
/^56$/ = BD
/^57$/ = BE
/^58$/ = BF
/^59$/ = BG
/^60$/ = BH
/^61$/ = BI
/^62$/ = BJ
/^63$/ = BK

/^64$/ = BL
/^65$/ = BM
/^66$/ = BN
/^67$/ = BO
/^68$/ = BP
/^69$/ = BQ
/^70$/ = BR
/^71$/ = BS
/^72$/ = BT
/^73$/ = BU
/^74$/ = BV
/^75$/ = BW
/^76$/ = BX
/^77$/ = BY
/^78$/ = BZ
/^79$/ = CA
/^80$/ = CB
/^81$/ = CC
/^82$/ = CD
/^83$/ = CE
/^84$/ = CF
/^85$/ = CG
/^86$/ = CH
/^87$/ = CI
/^88$/ = CJ
/^89$/ = CK
/^90$/ = CL
/^91$/ = CM
/^92$/ = CN
/^93$/ = CO
/^94$/ = CP
/^95$/ = CQ
/^96$/ = CR
/^97$/ = CS
/^98$/ = CT
/^99$/ = CU
/^100$/ = CV
/^101$/ = CW
/^102$/ = CX
/^103$/ = CY
/^104$/ = CZ
/^105$/ = DA
/^106$/ = DB
/^107$/ = DC
/^108$/ = DD
/^109$/ = DE
/^110$/ = DF
/^111$/ = DG
/^112$/ = DH
/^113$/ = DI
/^114$/ = DJ
/^115$/ = DK
/^116$/ = DL
/^117$/ = DM
/^118$/ = DN
/^119$/ = DO
/^120$/ = DP
/^121$/ = DQ
/^122$/ = DR
/^123$/ = DS
/^124$/ = DT
/^125$/ = DU
/^126$/ = DV
/^127$/ = DW
/^128$/ = DX
/^129$/ = DY
/^130$/ = DZ
/^131$/ = EA
/^132$/ = EB
/^133$/ = EC
...

 

I wrote a small bit of PHP to generate the regexes which I then copied and pasted in:

<?php
$z = 'A';
$sep = "";
for($a = 1; $a < 201; $a += 1){
        echo "\n/^$a$/ = $z";
        ++$z;
}
echo "\n\n\n";
?>

(Robin Bilney) #14

Hey Evan,

 

Where would you put the PHP code? I didn't know you could use custom PHP in matrix...


(Bart Banda) #15

I think he just meant that that's the code to generate the list so that you don't have to do it manually, then you put that list into the regex asset. 


(Robin Bilney) #16

Ah I see, thanks Bart.