Posted: Wed Feb 19, 2003 10:58 am Post subject: Strange behavior while looping
Novice
Joined: 21 Oct 2001 Posts: 21
I have a need to store pieces of a message into a database under certain conditions. I am trying to create a string of all the values of that section within a loop so as to avoid writing out each field. It appears as though the concatention is not working. Any help is appreciated. Here is the code:
SET g = CARDINALITY(InputRoot.MRM.INDV_REC.*[]);
WHILE i <= g DO
EVAL('SET v = InputRoot.MRM.INDV_REC.' || FIELDNAME(InputRoot.MRM.INDV_REC.*[i]));
SET ans = ans || v;
SET i = i + 1;
END WHILE;
If I put "ans" into the usr section of the RF2 header nothing appears. If I do a "bitstream" on ans and put that into the usr section I see data.
Any idea why the string concatenation is not showing up?
You shouldn't be using eval at all here - it's unecessary and slow.
You don't appear to have pasted the code where you initialized i (and if you're using v 2.1, you will see speed improvements if you use Reference variables instead of a Cardinality loop).
Are you trying to create a string made up of pieces like "<fieldname>.<fielddata>"?
Try something like this.
set i = 1;
set g= Cardinality(InputRoot.MRM.INDV_REC.*[]);
WHILE i <g DO
SET v = FIELDNAME(InputRoot.MRM.INDV_REC.*[i])||'.'||InputRoot.MRM.INDV_REC.*[i ];
set ans = ans||cast(v as character);
SET I=I+1;
END WHILE;
You may also be able to get what you by using just bitstream or asbitstream to flatten your tree, without having to use the loop or concatenation at all.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum