Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract data from string 3

Status
Not open for further replies.

kerrplunk

Technical User
Aug 11, 2009
4
GB
In CR10, is it possible to extract particular parts from a string?

I am trying to build a report that shows CIs that have changed location on our CMDB using the history line that is created when the field is updated which reads like:

Room No. from "4" to "6".
or
Room No. from "56" to "Store".

I was hoping to extract, into two seperate fields the bits between the double-quotes (ie the 4 and 6). To complicat ematters for me, the quoted data can be anything (text or numeric)up to 40 characters in length.

Any ideas? I've looked at various things like Split, but I get confused (easily!) by trying to include the quotes.

Thanks.
 
ok its really ugly but it will work

formula for piece one
numbervar a := instr({@yourfield},'"');
numbervar b := instr({@yourfield},'to');
stringvar pieceone := trim(replace(mid({@yourfield},a,(len({@yourfield})-b)-2),'"',""));
pieceone

formula for piece two
numbervar a := len({@yourfield});
numbervar b := instr({@yourfield},'to');
stringvar pieceone := trim(replace(replace(mid({@yourfield},b+2,a-b),'"',""),".",""));
pieceone



_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
Thanks for your help.

Piece two works great, but for piece one I get an error: "Start position is less than 1 or not an integer
 
is there always a value in that field? The only way I get that error is if I use a blank field or if the field does not have a " at all try adding the if statement to both formulas. It should read like this

numbervar a := instr({@yourfield},'"');
numbervar b := instr({@yourfield},'to');

if instr({@yourfield},'"') = 0 then "" else

stringvar pieceone := trim(replace(mid({@yourfield},a,(len({@yourfield})-b)-2),'"',""));
pieceone



_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
OK, I got the first piece working by chucking some brackets around it.

But it is now producing inconsistent results as it variously extracts the full remaining line of text (looks like if the quoted data isn't only a number) or stops when it gets to a space, or stops partway through a word (such as the inbetween 'to' which is after the second quote)

 
throw me out some more extreme examples of what your raw data looks like

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
I took another approach at your issue from the angle you were actually going with before I made it harder than it is. I initially looked at it and didnt think split would work because its a little different than data you would normally use split with. Buut I gave it a shot on the original example you gave and it works pretty well. I think where you may have been having problems was enclose the desired " for the split in ". you have to use single quotes.

Room No. from "4" to "6".

split({@yourfield},'"')[2]

results in 4


split({@yourfield},'"')[4]

results in 6

let me know if that works

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
That is great - it works brilliantly!

Now how does it work?!? (I think) I get the Splitting the field at the ", but where does the [2] and [4] come in to it (something to do with arrays)?
How does it know where to stop?

Thanks again.
 
the brackets indicate the index of the array. In the formula we used " as the character that separates the different values we wanted to assign the indexes to. So in your example when you use the split function it breaks the string down into an array with 5 indexes. The formula we created tells it to use [2] or [4].

Room No. from [1]
56 [2]
to [3]
Store [4]
. [5]

In my experience it is a little unusual to use the split in your scenario but it worked really well! I learned something from this one because I wasn't aware split would apparently assume the " at the beginning and end of the string.

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
If you think someone might find it helpful then sure!

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top