Use 's:strfmt' conveniently with a 'map' for input

  • 4 Replies
  • 1143 Views
*

ron

  • Administrator
  • Guru
  • *****
  • 3,238
Use 's:strfmt' conveniently with a 'map' for input
« on: January 08, 2017, 13:52:47 »
I'm working on something where I have a map with values, and I want to create a string with those values in certain places.  Just what "s:strfmt" is good for.

But "s:strfmt" wants the items in a particular order, so I need to put the values in an array (or arrange them on the stack in the right order).  An array is convenient, and so is this helper word:
Code: [Select]
: s:strfmap \ map keys fmt -- map str
  >r ' m:@ a:map r>
  s:strfmt ;
This new word takes a map, an array of strings which are keys for the values in the map which you want, and 'fmt' is the usual s:strfmt format string.

Example:

{ "name": "Ron" , "email" : "private" }
["name", "email"]
"Hi, %s.  We're sending a mail to %s"
s:strfmap

This is a simple example, but it shows the idea.  If you have more than a couple format specifiers in the map, this word is worth the effort.

A particularly nice trick the conversion of the array of keys, into an array of the values those keys represent in the map.

*

RichAMead

  • beta
  • Guru
  • *****
  • 593
  • "We all live in a big black hole. No, really."
Re: Use 's:strfmt' conveniently with a 'map' for input
« Reply #1 on: January 08, 2017, 16:00:26 »
I thought s:tsub took care of this for us...?  Although I suppose without the benefit of formatting non-string values (%d, %f, %x)

*

ron

  • Administrator
  • Guru
  • *****
  • 3,238
Re: Use 's:strfmt' conveniently with a 'map' for input
« Reply #2 on: January 08, 2017, 16:40:52 »
Yes, and yes :)

*

hotcore

  • Master
  • ****
  • 276
Re: Use 's:strfmt' conveniently with a 'map' for input
« Reply #3 on: January 09, 2017, 12:09:38 »
Forgive this n00b observation :)

In the code it reads:
   >r ' m:@ a:map r>

The second word appears to be the G:' word isn't it?

I searched the manual for that ' thingy and found out that you call it the "tick".
My problem was that I was searching for the term "single quote".

Maybe you could use that description as an extra in the manual together with "tick",
to make it easier to find (for n00bs that is) ...

Also I gather that there are 2 purposes for the tick. In JSON it is something different, together with backtick isn't it?
Haven't studied that yet. though.

*

ron

  • Administrator
  • Guru
  • *****
  • 3,238
Re: Use 's:strfmt' conveniently with a 'map' for input
« Reply #4 on: January 09, 2017, 12:17:58 »
Yes, the single-quote character is a word, and that word is usually called 'tick', but it does make sense to also cross-reference it as 'single-quote'.  Sorry for that confusion.

No, in JSON (as expanded by 8th), the single-quote (or 'tick') has the same purpose as outside JSON, e.g. it looks at the following text and returns the word corresponding to it (think of it as giving a function-pointer back).

The backtick (`) word is an inline-eval operator, and it also works the same inside and outside of JSON.  Just that it's more commonly used inside JSON.