Looping algorithm to generate a string...

This is a discussion on Looping algorithm to generate a string... within the alt.comp.lang.php forums, part of the PHP Programming Forums category; This is totally stupid but...I want to generate a string like: "Showing all assets with a status of ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-19-2006
rlee0001
 
Posts: n/a
Default Looping algorithm to generate a string...

This is totally stupid but...I want to generate a string like:

"Showing all assets with a status of Ordered, Received or Placed."

The statuses are stored in an array and any number of them can be
expected (there are like 15 possible values and they are specifically
ordered). So the problem is that I want commas between the items except
the last two. I want an 'or' there.

Now I know I can code this up by looking ahead and it'll look very
messy but is there a known-optimal way to do this? Maybe counting the
records first and using a FOR loop for the first n-2 then outputing the
last two with the OR between them?

Anyone got the "perfect" answer?

What if you don't have an array but rather a bunch of seperate
variables like $name, $address, $age and so on? Whats the best way to
do this:

"Robert, Austin and 18 years old."

....when any of those variables are NULL or '' they are skipped so this
might also be output:

"Robert and Austin."

Like I said, its a stupid question.

-Robert

Reply With Quote
  #2 (permalink)  
Old 01-19-2006
ZeldorBlat
 
Posts: n/a
Default Re: Looping algorithm to generate a string...


rlee0001 wrote:
> This is totally stupid but...I want to generate a string like:
>
> "Showing all assets with a status of Ordered, Received or Placed."
>
> The statuses are stored in an array and any number of them can be
> expected (there are like 15 possible values and they are specifically
> ordered). So the problem is that I want commas between the items except
> the last two. I want an 'or' there.
>
> Now I know I can code this up by looking ahead and it'll look very
> messy but is there a known-optimal way to do this? Maybe counting the
> records first and using a FOR loop for the first n-2 then outputing the
> last two with the OR between them?
>
> Anyone got the "perfect" answer?


I don't know if it's perfect, but it'll work. And it isn't /that/
stupid :)

$a = array('Ordered', 'Received', 'Placed');
$str = implode(', ', $a);
$pos = strrpos($str, ',');
$str = substr($str, 0, $pos) . ' or' . substr($str, ($pos+1));
$str = "Showing all assets with a status of $str";

Of course, that doesn't account for the corner cases of 0 or 1 items in
the array.

Reply With Quote
  #3 (permalink)  
Old 01-19-2006
rlee0001
 
Posts: n/a
Default Re: Looping algorithm to generate a string...

Thats pretty cool. I didn't even think of implode and would have used a
loop.

Maybe I'll write a function that uses implode along with your code to
insert 'or' and a check for the possibility of 0 or 1 array element.
Something like:

explode_x (seperator, array)

....where seperator is something like 'and' or 'or'. I ultimately want
to generate a human readable paragraph which describes an advanced
search which contains multiple fields, some of which are arrays.



Showing all assets with a status of Ordered, Received or Placed,
assigned to a user whose last name begins with either 'Smith' or 'D'
and first name begins with 'J' on purchase order 374238956.



Notice that although the purchase order was the last field in the list
that 'and' was not used but rather 'on'. And that since last name was
an array an 'either' was inserted while an either was not inserted in
first name since it contained only one value. But status doesn't get an
'either' either way. Also last name and first name were seperated by an
'and' even though first name isn't the last field in the list. And
althought last name and first name can be specified independantly of
one-another there is only one prefix 'where the user whose' included
before the both of them.

And equally valid output for a different set of specified fields could
have been:



Showing all assets of type Desktop or Laptop, assigned to a user whose
first name begins with 'J', with a MAC address which starts with
'de:ad:be' and which counts as a seat.



Any field can be omitted and other fields may have been specified.
Something this complex might be impossible. But I'm sure that where
there is a will there is a way.

Has anyone done anything like this before. <-- Another stupid question.

-Robert

Reply With Quote
  #4 (permalink)  
Old 01-21-2006
Balazs Wellisch
 
Posts: n/a
Default Re: Looping algorithm to generate a string...


"rlee0001" <robeddielee@hotmail.com> wrote in message
news:1137633575.690364.86380@g47g2000cwa.googlegro ups.com...
> This is totally stupid but...I want to generate a string like:
>
> "Showing all assets with a status of Ordered, Received or Placed."
>
> The statuses are stored in an array and any number of them can be
> expected (there are like 15 possible values and they are specifically
> ordered). So the problem is that I want commas between the items except
> the last two. I want an 'or' there.


How about...

$array = array('Ordered', 'Received', 'Placed');
$list = join($array, ', ');
$lastComma = strrpos($list, ',');

print substr_replace($list, ' or', $lastComma, 1);

Balazs





Reply With Quote
  #5 (permalink)  
Old 02-05-2006
Jim Michaels
 
Posts: n/a
Default Re: Looping algorithm to generate a string...


"rlee0001" <robeddielee@hotmail.com> wrote in message
news:1137633575.690364.86380@g47g2000cwa.googlegro ups.com...
> This is totally stupid but...I want to generate a string like:
>
> "Showing all assets with a status of Ordered, Received or Placed."


current grammar rules when I was in college 10 years ago was that it should
be
"Showing all assets with a status of Ordered, Received, or Placed."

>
> The statuses are stored in an array and any number of them can be
> expected (there are like 15 possible values and they are specifically
> ordered). So the problem is that I want commas between the items except
> the last two. I want an 'or' there.
>
> Now I know I can code this up by looking ahead and it'll look very
> messy but is there a known-optimal way to do this? Maybe counting the
> records first and using a FOR loop for the first n-2 then outputing the
> last two with the OR between them?
>
> Anyone got the "perfect" answer?
>
> What if you don't have an array but rather a bunch of seperate
> variables like $name, $address, $age and so on? Whats the best way to
> do this:
>
> "Robert, Austin and 18 years old."
>
> ...when any of those variables are NULL or '' they are skipped so this
> might also be output:
>
> "Robert and Austin."
>
> Like I said, its a stupid question.
>
> -Robert
>



Reply With Quote
  #6 (permalink)  
Old 02-05-2006
Jim Michaels
 
Posts: n/a
Default Re: Looping algorithm to generate a string...


"Jim Michaels" <jmichae3@nospam.yahoo.com> wrote in message
news:E5mdneObcL9xHXjenZ2dnUVZ_tKdnZ2d@comcast.com. ..
>
> "rlee0001" <robeddielee@hotmail.com> wrote in message
> news:1137633575.690364.86380@g47g2000cwa.googlegro ups.com...
>> This is totally stupid but...I want to generate a string like:
>>
>> "Showing all assets with a status of Ordered, Received or Placed."

>
> current grammar rules when I was in college 10 years ago was that it
> should be
> "Showing all assets with a status of Ordered, Received, or Placed."


given the earlier code shown, it's an easy mod (needs a space, too).
$array = array('Ordered', 'Received', 'Placed');
$list = join($array, ', ');
$lastComma = strrpos($list, ',');
print substr_replace($list, ', or ', $lastComma, 1);



>
>>
>> The statuses are stored in an array and any number of them can be
>> expected (there are like 15 possible values and they are specifically
>> ordered). So the problem is that I want commas between the items except
>> the last two. I want an 'or' there.
>>
>> Now I know I can code this up by looking ahead and it'll look very
>> messy but is there a known-optimal way to do this? Maybe counting the
>> records first and using a FOR loop for the first n-2 then outputing the
>> last two with the OR between them?
>>
>> Anyone got the "perfect" answer?
>>
>> What if you don't have an array but rather a bunch of seperate
>> variables like $name, $address, $age and so on? Whats the best way to
>> do this:
>>
>> "Robert, Austin and 18 years old."
>>
>> ...when any of those variables are NULL or '' they are skipped so this
>> might also be output:
>>
>> "Robert and Austin."
>>
>> Like I said, its a stupid question.
>>
>> -Robert
>>

>
>



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:32 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0