On Thu, Mar 07, 2002 at 04:59:54PM -0500, Charles Lu wrote: > I am trying to think of a quick and simple way to find out if a number > appears only in consecutive fashion once. In other words, a function that > will return true if a particular number in a list appears only > consecutively once. > > For example: Does "2" appear only in consecutive fashion? > > (1,2,2,2,3) = True;
This result is inconsistent with your description and your further examples; 2 occurs twice. I'll assume this example is incorrect. > (1,2,2,3,4,2) = False > > (1,2,2,3,4,2,2,) = False <-- consecutive but appears more than once > > (1,2,3,4,5,2,2,2,2,2) = False ( see previous reason) > > (1,2,3,4) = true <--- appearing only once is okay I used a subroutine because of the ease of returning a value. You should adapt it to taste. sub is_consecutive { my $last = shift; foreach my $num (@_) { return 0 if $num != $last + 1; $last = $num; } return 1; } You didn't include an example such as: 1,2,4,5 but you said consecutive, so that list must result in a false, and does with the code I showed. Also, none of your examples included fractional numbers (e.g. 1.2, 4.5, etc.) so I assumed you're only dealing with integers. Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]