Author |
Message
|
vijsam |
Posted: Thu Feb 02, 2012 7:16 am Post subject: count related function in esql |
|
|
Apprentice
Joined: 01 Jun 2011 Posts: 46
|
Hello All,
I need to count how many times a particular character in a string is repeatating,
example want to calculate the number of commas in value like:sam,ram,mbm. need your asisatnce. |
|
Back to top |
|
 |
Vitor |
Posted: Thu Feb 02, 2012 7:26 am Post subject: Re: count related function in esql |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
vijsam wrote: |
I need to count how many times a particular character in a string is repeatating, |
AFAIK there's no supplied function that does this in ESQL. It would be a trivial task to write your own (I came up with 2 possible methods in 1 sip of coffee).
I'll leave others to comment on the possibilities Java offers. I would imagine there's some kind of tokenising facility & you could count the tokens but I imagine a lot of things that turn out not to be true. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
kash3338 |
Posted: Thu Feb 02, 2012 7:26 am Post subject: Re: count related function in esql |
|
|
Shaman
Joined: 08 Feb 2009 Posts: 709 Location: Chennai, India
|
vijsam wrote: |
I need to count how many times a particular character in a string is repeatating,
example want to calculate the number of commas in value like:sam,ram,mbm. need your asisatnce. |
You can write a simple function for that in ESQL with a IN parameter as the character to be checked and OUT parameter as the count. |
|
Back to top |
|
 |
lancelotlinc |
Posted: Thu Feb 02, 2012 7:28 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
|
Back to top |
|
 |
mqsiuser |
Posted: Thu Feb 02, 2012 7:41 am Post subject: |
|
|
 Yatiri
Joined: 15 Apr 2008 Posts: 637 Location: Germany
|
Code: |
CREATE FUNCTION countCharInChar(IN char1 CHAR, IN char2 CHAR) RETURNS INT
-- Returns the number of occurences of char1 in char2
-- E.g.: ('A', 'ABCDABCDABCD') --> 3
-- ('ABC', 'ABCDABCD') --> 2
-- ...
BEGIN
DECLARE count INT 0;
IF char1 IS NOT NULL AND char2 IS NOT NULL THEN
DECLARE position1 INT POSITION( char1 IN char2 REPEAT count+1 );
WHILE position1 <> 0 DO
SET count = count + 1;
SET position1 = POSITION( char1 IN char2 REPEAT count+1 );
END WHILE;
END IF;
RETURN count;
END; |
Simple things should also be done with care ! _________________ Just use REFERENCEs |
|
Back to top |
|
 |
adubya |
Posted: Thu Feb 02, 2012 7:53 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
SET tmpString = REPLACE ( originString, ',' );
SET count = LENGTH ( originString) - LENGTH (tmpString);
or
SET count = LENGTH ( originString) - LENGTH ( REPLACE ( originString, ',' ) );
There are many ways to skin a cat.
Method #54: Sandblasting. |
|
Back to top |
|
 |
mqsiuser |
Posted: Thu Feb 02, 2012 8:32 am Post subject: |
|
|
 Yatiri
Joined: 15 Apr 2008 Posts: 637 Location: Germany
|
adubya wrote: |
There are many ways to skin a cat. |
We are searching for the fastest (and most appropriate) approach.
Your approach looks elegant, but manipulating strings (changing a string) can be expensive in memory-space and processing-time. That's why Java has something called a StringBuffer/Builder. The wiki-page says: "In order to insert or remove characters at arbitrary positions, whole sections of arrays must be moved". It works similar with C/ESQL... there actually is a best practice for performance in Broker, which is "try to avoid string manipulation".
Looping with increasing the pos-counter might also get slow (for large strings), I must admit. _________________ Just use REFERENCEs |
|
Back to top |
|
 |
vijsam |
Posted: Fri Feb 03, 2012 12:49 am Post subject: |
|
|
Apprentice
Joined: 01 Jun 2011 Posts: 46
|
Thanku all for the valuable inputs  |
|
Back to top |
|
 |
Vitor |
Posted: Fri Feb 03, 2012 6:01 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
lancelotlinc wrote: |
Or Java Compute Node. |
For the record, I deliberately said Java. Perhaps even called as a function from ESQL if you don't want to go JCN.
Is there some kind of tokenizing thing? Not as an answer to the OP but just to see if I was imagining that & need to adjust my medication. Again. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
kimbert |
Posted: Fri Feb 03, 2012 6:07 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Vitor: Java cannot tokenize stuff. Ever. Please adjust your medication.  |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Feb 03, 2012 6:33 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
kimbert wrote: |
Vitor: Java cannot tokenize stuff. Ever. Please adjust your medication.  |
No, no. Vitor needs *more* Java...  |
|
Back to top |
|
 |
Vitor |
Posted: Fri Feb 03, 2012 6:51 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
kimbert wrote: |
Vitor: Java cannot tokenize stuff. Ever. Please adjust your medication.  |
Just when I think I'm getting somewhere....  _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Feb 03, 2012 6:55 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
|
Back to top |
|
 |
|