|
|
| Author |
Message |
vvmanyam
New User
Joined: 16 Apr 2008 Posts: 37 Location: Bangalore
|
|
|
|
Hi friends,
I have a small requirement I want to Unstring a variable Delimited by ','
But I don't know how many commas will be there in the string
Sample Input with 2 records in it:
| Code: |
1330,0,0,158,0,0,158,0,0,158,0,0,158,0,0,158,0,0,158,0,1030,1085,870,1030,1085,870,1030,0,335,349,0,335
335,349,298,335,349,298,335,349,298,335,349,298,335,34,349,0,335,349,0,335,349,0,335,349,0,335,349,0,335,349,0,335
|
Input file will look like this
Each field refer to some value for a month.
So, It might have data for any no of months(Max of 1000).
But no of months in every row will be same.
Now I want to Extract each record in to a table of array.
I can't use this as I have to write all the thousand out-variables.
| Code: |
UNSTRING Input-Var Delimited by ','
into out-var(1)
into out-var(2)
into out-var(3)
into out-var(4)
.......
...and so on...
into out-var(1000)
END-UNSTRING
|
How can I acheive this?
Hope I am clear
Thanks,
Balu |
|
| Back to top |
|
 |
References
|
|
 |
dick scherrer
Global Moderator
Joined: 23 Nov 2006 Posts: 9191 Location: 221 B Baker St
|
|
|
|
Hello,
| Quote: |
I can't use this as I have to write all the thousand out-variables.
|
Why is that a reason that you cannot use this? If there 1000 source fields, you must provide for getting them into individual target fields. |
|
| Back to top |
|
 |
vvmanyam
New User
Joined: 16 Apr 2008 Posts: 37 Location: Bangalore
|
|
|
|
Hi,
I can use it.
But I am trying for any other solution using loops to unstring one month at a time and populate it to the required variable.
But I am unable to get the desired result.
If I Can split this string to be split into 2 varaibles, then I can use Ustring in a loop and ustring the second variable each time to get the first months value to the required variable.
| Code: |
1330,0,0,158,0,0,158,0,0,158,0,0,158,0,0,158,0,0,158,0,1030,1085,870,1030,1085,870,1030,0,335,349,0,335
|
One varaiable with first month qty and the rest in to different variables
First variable:
Second variable:
| Code: |
0,0,158,0,0,158,0,0,158,0,0,158,0,0,158,0,0,158,0,1030,1085,870,1030,1085,870,1030,0,335,349,0,335
|
Can this be done?
Thanks,
Balu |
|
| Back to top |
|
 |
Sambhaji Warnings : 1 Active User
Joined: 16 Feb 2007 Posts: 265 Location: Pune, India
|
|
|
|
| Quote: |
One varaiable with first month qty and the rest in to different variables
First variable:
Code:
1330
Second variable:
Code:
0,0,158,0,0,158,0,0,158,0,0,158,0,0,158,0,0,158,0,1030,1085,870,1030,1085,870,1030,0,335,349,0,335
Can this be done?
|
Yes this can be achieved using below code. Then you can use below in loop to achieve your original task
| Code: |
MOVE "1330,0,0,158,0,0,158" to STRWITHCOMMA.
unstring strwithcomma delimited by ','
into strfirst
count in strcnt.
move ',' to strfirst(strcnt + 1:strcnt + 1).
unstring strwithcomma delimited by strfirst(1:strcnt + 1)
into strrest
strrest.
DISPLAY strfirst(1:strcnt).
DISPLAY strrest.
|
Output will be:
| Code: |
1330
0,0,158,0,0,158
|
|
|
| Back to top |
|
 |
dick scherrer
Global Moderator
Joined: 23 Nov 2006 Posts: 9191 Location: 221 B Baker St
|
|
|
|
Hello,
| Quote: |
| But I am trying for any other solution using loops to unstring one month at a time and populate it to the required variable. |
Is thsi just to save you a little editing time?
If your file is or will become high-volume, looping will just waste a lot of machine time. Maybe no one managing your system is concerned about resources wasted, but many system mamagers are. The cost of upgrading cpu's due to wasteful programming often not acceptable.
It is even worse when it is just due to a coder being lazy.
As i mention fairly regularly, just because something can be coded some poor-choice way is no reason that the poor choice should be made.
FWIW - the 3 main considerations for code that is to be promoted to production are:
It must run correctly every time
It must only use a reasonable amount of resource (i/o and/or cpu)
It must be easily maintainable. |
|
| Back to top |
|
 |
vvmanyam
New User
Joined: 16 Apr 2008 Posts: 37 Location: Bangalore
|
|
|
|
Thanks Sambhaji for ur solution
Thanks Dick for your valuable advice
I am not a lazy programer but I was curious to know how it can be done
and that too my max no of months are only 40, So, I have already coded it in the manner what I have specified in my first post.
But Slight correction in the code what Sambhaji has given
| Code: |
strfirst(strcnt + 1:strcnt + 1)
|
I think it should be:
| Code: |
strfirst(strcnt + 1:1)
|
Thanks,
Balu |
|
| Back to top |
|
 |
dick scherrer
Global Moderator
Joined: 23 Nov 2006 Posts: 9191 Location: 221 B Baker St
|
|
|
|
Hello,
| Quote: |
| I was curious to know how it can be done |
Yes, i too believe curious is good. It leads to many interesting discoveries
| Quote: |
But Slight correction in the code what Sambhaji has given
|
Possibly. . . Unfortunately, a lot of the code posted is not tested - it is more of "i think this will work". Fortunately, we do have many that post tested code and the result of the tested code from an actual execution. Maybe Sambhaji will have time to compile/test/and post the results here.
Good luck
d |
|
| Back to top |
|
 |
Sambhaji Warnings : 1 Active User
Joined: 16 Feb 2007 Posts: 265 Location: Pune, India
|
|
|
|
| Balu wrote: |
But Slight correction in the code what Sambhaji has given
Code:
strfirst(strcnt + 1:strcnt + 1)
I think it should be:
Code:
strfirst(strcnt + 1:1)
|
No it is correct. This statement is required to add ',' at end of string.
e.g. here in above example after unstring strfirst will have 1330 and strcnt will have 4 so i want ',' to be added at 5th position.
| Dick wrote: |
Maybe Sambhaji will have time to compile/test/and post the results here
|
Hi Dick, Output i have posted is tested.
The care Balu has to to be taken when he will put this in loop will be
moving strrest to strwithcomma and break the loop if strfirst is blank after unstring. |
|
| Back to top |
|
 |
Sambhaji Warnings : 1 Active User
Joined: 16 Feb 2007 Posts: 265 Location: Pune, India
|
|
|
|
May be putting above in loop will be complex.
below is code in loop using one unstring
| Code: |
01 STRWITHCOMMA PIC X(20).
01 STRfirst PIC X(20).
01 STRrest PIC X(20).
01 strcnt PIC 99.
01 strend PIC 99.
'
'
'
MOVE "1330,1,0,158,0,0,15 " to STRWITHCOMMA.
perform find-first
until strcnt = (function length(strwithcomma) + 2).
'
'
find-first.
unstring strwithcomma delimited by ','
into strfirst
count in strcnt.
compute strcnt = strcnt + 2.
compute strend = 20 - strcnt + 1.
move strwithcomma(strcnt:strend) to strrest.
DISPLAY 'strfirst is:' strfirst.
move strrest to strwithcomma.
|
Output is:
| Code: |
strfirst is:1330
strfirst is:1
strfirst is:0
strfirst is:158
strfirst is:0
strfirst is:0
strfirst is:15
|
|
|
| Back to top |
|
 |
|
|
|