Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: to convert a word to its number equivalent Post Reply Post New Topic
Author Message
johnwsun
Senior Member
Senior Member


Joined: 28 May 2008
Location: Australia
Online Status: Offline
Posts: 179
Quote johnwsun Replybullet Topic: to convert a word to its number equivalent
     Posted: 11 May 2009 at 5:49pm
Hi All,

There are book titles like the following when sorting by ascending, they become:
xxxx Book Five
xxxx Book Four
xxxx Book One
xxxx Book Three
xxxx Book Two

xxxx Volume 1
xxxx Volume 10
xxxx Volume 2
xxxx Volume 3 etc

'xxxx' are same titles, but with different volume etc.
Obviously, this is not the customer want. Could somebody advise if CR XI can sort the above in a correct order by converting Five to 5 etc.
By the way these books are displayed in the detail secion.
Book Four, Volumn1 may not neccessary be at the end of the title.
Could somebody have experience in this aspect please advise how to ahieve this?  Thanks in advance.
John
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 12 May 2009 at 6:49am
nothing is going to be be fool proof.  you could write a formula that converts the words to numbers and order by that, but then you might get the second example and an ASCII sort will not work, unless you start prefacing with 0's but that will look stupid, and how many to you need? 
 
You might try spaces instead of 0's , that wouldn't look as stupid, and it should sort correctly, but can you be assured that volumes will be less than 100?
 
As to location of the word that you want to convert, the question becomes do you want to replace the word wholesale (any One becomes 1) which might yield inaccurate results or does it need to be preceded by Book or Volume?  Either way you would use a formula like:
 
 REPLACE(lcase({table.field}), "one", "1");
 
this would be the wholesale solution, but you can easily alter it to include the word book like:
 
 REPLACE(lcase({table.field}), "book one", "Book 1");
 
there would be a lot of these, but it would all be in 1 formula.  If you wanted you could display this formula instead of the original title from the database and you should be able to order the group by it.  Extra spaces can be added into the last section if you want that as well.
 
HTH
 
 
 
IP IP Logged
johnwsun
Senior Member
Senior Member


Joined: 28 May 2008
Location: Australia
Online Status: Offline
Posts: 179
Quote johnwsun Replybullet Posted: 13 May 2009 at 3:16am

Hi Lockwelle,

Thansk for your suggestion. I'm afraid the customer may not like Book 1 instead of Book one which is the 'real' title.

I was using the following coversion routine obtained from SAP CR forum, the result of displaying is just as what you have said.

The codes:

local stringvar array toclean ;// text to delete into title
local stringvar array numberlist ;// list number in text in
local stringvar sortvolume;
local numbervar sortnum;
local numberVar counter;
local stringVar booktitle;
local stringVar booktitleb;
 
 
numberlist:=["zero","one","two","three","four","five","six"];
toclean:=["Book","volume","episode"];
sortvolume:={HEADING.HEAD};//book title
 
// find book title and order infos
for counter:=1 to UBound(toclean) step 1 do
(
    if lowercase(toclean[counter]) in lowercase(sortvolume) then
    (
    booktitle:= left(sortvolume,(InStr(lowercase(sortvolume),
lowercase(toclean[counter])))-2);
    booktitleb:=mid(sortvolume,InStr(lowercase(sortvolume),
lowercase(toclean[counter])));
 
    exit for
    )
);
 
 
// delete words from toclean into sortvolume
for  counter:=1 to UBound(toclean) step 1 do
booktitleb:= trim(replace(booktitleb,toclean[counter],"",1,-1,1));
if isnumeric(booktitleb)=true then sortnum:=ToNumber (booktitleb);
 
 
//look up if words from numberlist is equal to sortvolume if yes allocate number at place
for counter:=1 to UBound(numberlist) step 1 do
(
    if numberlist[counter]=booktitleb then
    (
    sortnum:=counter-1;
    exit for
    )
);
 
 
//create sort field with book title and sortnum
if sortnum<10
then  booktitle+" 0"+totext(sortnum)
else
booktitle+" "+totext(sortnum)

 
 

 

 

The result of display in the report:
 
Great stories for kids. 01.00
Great stories for kids. 02.00
Great stories for kids. 03.00
 
but titles with 'Volume'  became:
The Bible story. 00.00
The Bible story. 00.00
The Bible story. 00.00
The Bible story. 00.00 etc
 
The actutal location of Volume X is in the middle of the title:
The Bible story. Volume 1, The book of beginnings
The Bible story. Volume 10, Onward to glory
The Bible story. Volume 2, Mighty men of old
 
Any advices on that? Thanks a lot!
 
John
 
 


Edited by johnwsun - 13 May 2009 at 3:18am
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 13 May 2009 at 6:30am
Much more complex and hard to trace through.  Since the word volume is in the middle, it takes the value after the key word and converts it to a number, but numbers and letters don't always translate the way you expect...hence the 0.00.  It is a parsing issue that is not easy to solve. My hint to attack this would be to try and isolate the number value that follows the key word from any other text that may follow the number.
 
Personally, I would have used spaces instead of the leading 0 as the titles would better, but for sorting it makes no difference.
 
A question that I am not sure about is why the word Volume is gone.  I would build several other formulas, that look just like this one, but stop as different locations so that I would be able to 'see' what the formula is doing and then modify as needed.
IP IP Logged
johnwsun
Senior Member
Senior Member


Joined: 28 May 2008
Location: Australia
Online Status: Offline
Posts: 179
Quote johnwsun Replybullet Posted: 24 May 2009 at 4:32am

Hi Lockwelle,

I think I should give it up. thanks for the help anyway.

John

IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum