General discussion

Locked

Inserting commas in text lists

By MaryWeilage Editor ·
The May 13 Perl TechMail described how to insert commas in text lists. Will you use the technique explained in the tip to insert commas in text lists? Tell us how you plan to use it--or tell us why you don't plan to use it.

If you haven't subscribed to the Perl TechMail, there is no better time to take advantage of our free e-newsletter. Visit our e-newsletter subscription center to subscribe to this valuable TechMail today:
http://builder.com.com/techmails.jhtml?repID=u001

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Huh?

by twaks In reply to Inserting commas in text ...

Once again, the code that is shown in this tip is absolutely hideous! I have been using PERL for years, and I cannot understand a single line provided in this sample.

Collapse -

maybe I can help

by swstephe In reply to Huh?

having tackled linguistic problems before, (take a look at the code for "Plural" to appreciate how complex English is). The examples could have been shown in code:

print EnglishCommas('Dilbert') => "Dilbert"
print EnglishCommas('Bilbo','Frodo')=> "Bilbo and Frodo"
print EnglishCommas('The butcher', 'the baker', 'the candlestick maker') => "The butcher,the baker and the candlestick maker"
print EnglishCommas('Pimento cheese','peanut butter and jelly', 'egg salad', 'bacon, lettuce, and tomato') => "Pimento cheese,peanut butter and jelly, egg salad and bacon, lettuce and tomato"

Does that help?

Collapse -

Code clarification

by James Brown In reply to Huh?

I apologize if the code in the tip was unclear. Let me see if I can clarify what the code is doing.

The first section creates a "list of lists" or an "array of arrays" if you will. This is only used for testing and would not appear in your normal code. Essentially, you have an array, @lists, which has as its elements anonymous arrays. Each anonymous array contains a list of strings.

For example, $lists[1] would be an anonymous array containing the elements 'Bilbo' and 'Frodo'. The element $lists[1][1] would be 'Frodo'.

Moving on down the code, the for loop simply iterates through the "list of lists" and processes each anonymous array through the EnglishCommas function. The code inside the for loop dereferences the outer part of the lists array and passes each of the anonymous arrays to the function in turn.

Lastly, we have the 'meat' of the code, the body of the EnglishCommas function. First, the routine determines what the separator should be. If any of the elementsof the passed array contain a comma, then the separator should be a semicolon. Otherwise we use a comma as the separator.

Next the routine identifies three special cases. First, if there are no parameters then we simply return an empty string.Second, if there is exactly one parameter the return value is simply that single parameter. Lastly, if there are exactly two parameters then the return value is the first parameter, the word ‘and’ and the second parameter.

Having tested for all special cases, it is now time to process a list with three or more elements. To do this, the routine essentially creates a temporary array to pass to the internal function ‘join’. This temporary array is all the elements of the array except the last one, plus the string ‘and ’ prepended to the last element. The join function then inserts the predetermined separator between all its paramete

Back to Web Development Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums