1

I have a set of items that I want to put in an string but i don't know the order or the right format, and I am letting the "user" define this configuration, then I want to parse this expression and return an string given a set of objects.

examples...

given the set of elements [a1, a2, a3, a4] and some expressions

"Printing items: (item)[0] (, item)[2,n-1] and (item)[n]" 

produces

"Printing items: a1, a2, a3 and a4"

Another example:

"Another way: we have (item)[0] and {n-1} others (</br> item)[2,n-1] </br> and (item)[n]"

results is

Another way: we have a1 and 3 others 
a2
a3
and a4.

I have employed a custom notation, but maybe there is something out there that can help me or maybe an standard notation already done for this. Any ideas? What the best way of approaching to a solution.

Thanks.

k-dev
  • 1,657
  • 19
  • 30

3 Answers3

1

Typical string parsing.

I'd say remove the reference to item unless you have multiple lists. Then include an escape character.

Printing items: #item#0#, #item#2,%n-1%,","# and #item#%n%#

Another way: we have #item#0# and %n-1% others #item#2, %n-1%, ""# and #item#%n%#

This would be much easier to parse.

Anything between % is a calculation, # denotes a reference. If the reference is to a list, the second # and third # enclose a index. Comma indicates a range. Second Comma indicates a separation character.

Add a hash table that converts user strings to actual references. Derive everything the user can reference from the same base class, or have everything implement an interface.

Search for your escape character, if it is a reference to an array, look to see if there is more of that escape character, if you find a comma before the 3rd escape character you have a range, second comma and you have an item separator.

Then you can just tokenize the string, instead of interpreting braces.

Lee Louviere
  • 5,162
  • 30
  • 54
  • I like your idea of # to simplify, but it took me a while to understand it, seems less human readable. Also you include a separator as 3rd argument but is not a separator what i put inside () is more like a template. Given your answer seems i need to build my own parser... – k-dev Jan 27 '11 at 23:35
1

It sounds like you want to provide a user definable formatting mechanism ... if not then disregard the rest of this answer :)

I can think of three possible solutions depending on how flexibility you want to offer and how much effort you want to put in.

1) The simplest and least flexible solution is to use string.Format(). This will allow users to decide what items to show and in what order, but requires the user reference each value by index instead of name and cant handle data sets of arbitrary. Also you would need to handle the FormatException if the user tries to reference a non-existent value.

2) Another possibility is to produce the data in a stable xml format and let the user provide an xslt. This works best for a small number of large output's as the xslt can be a bit long-winded for a large number of simple outputs. The System.Xml.Xsl name space seems to provide an Xslt engine but I have never tried to used it.

3) You could use a .NET template engine. there is another SO question regarding template engines in .NET but I have never used any of them myself.

Can you recommend a .net template engine?

Community
  • 1
  • 1
Brian Reichle
  • 2,798
  • 2
  • 30
  • 34
  • @brian-reichle that's the idea, but string.format and xslt can't be used for reasons you exposed, seems template engines are focused on asp.net. I want to use it for print a list of objects in different languages with different formatting. – k-dev Jan 28 '11 at 17:20
0

I may be missing what's being asked here, but I would do the following:

    Console.writeLine(messageToBePrinted);   
for(int i = 0;i<array.size;i++)  
{
  if(i == array.size -1){
    Console.writeLine(endFormat);  
 }
  else{
  Console.writeLine(normalFormat);  
 }
  Console.writeLine(array[i]);  
}
Woot4Moo
  • 23,987
  • 16
  • 94
  • 151