Common Variables and Dynamic Arrays

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
mleroux
Participant
Posts: 81
Joined: Wed Jul 14, 2004 3:18 am
Location: Johannesburg, South Africa
Contact:

Common Variables and Dynamic Arrays

Post by mleroux »

I need to create a dynamic array once and re-use it for every row of data.

I have these routines:

BussOrIndPopArray

Code: Select all

common /BussOrIndv/ a, aNumVals

* The second field in the array denotes where the substring
* will occur: l = Left, r = Right, a = Anywhere in the str.

a = ""
a<-1> = "engineering" : @VM : "a"
a<-1> = "inc." : @VM : "r"
a<-1> = "limited" : @VM : "r"
a<-1> = "stores" : @VM : "a"
a<-1> = "consult" : @VM : "a"
etc...

aNumVals = dcount(a, @FM)

Ans = "Y"
BussOrIndvClassify

Code: Select all

common /BussOrIndv/ a, aNumVals

Ans = aNumVals
return(Ans)

CleanEntityName = downcase(trim(EntityName))
found = 0
for i = 1 to aNumVals
  begin case
    * If the occurrence may be anywhere in the string...
    case a<i,2> = "a"
      found = index(CleanEntityName, a<i,1>, 1)
    * If the occurrence must be at the left of the string...
    case a<i,2> = "l"
      found = index(left(CleanEntityName, len(a<i,1>)), a<i,1>, 1)
    * If the occurrence must be at the right of the string...
    case a<i,2> = "r"
      found = index(right(CleanEntityName, len(a<i,1>)), a<i,1>, 1)
  end case
  if found > 0
  then
    Ans = "B"
    exit
  end
next i

if found = 0
then
  Ans = "I"
end

return(Ans)
All the above code works perfectly if I create the array and use it in the same routine, but of course I don't want to create it with every row of data, so I am now trying to call the BussOrIndPopArray routine as either a Before/After routine or in a stage variable, like this:

Code: Select all

if ArrayCreated = "Y" then "Y" else BussOrIndvPopArray("DummyArg")
When I do either of these, the common variables are seemingly empty. Any help is welcome, please.
Morney le Roux

There are only 10 kinds of people: Those who understand binary and those who don't.
chucksmith
Premium Member
Premium Member
Posts: 385
Joined: Wed Jun 16, 2004 12:43 pm
Location: Virginia, USA
Contact:

Post by chucksmith »

When using named common, I initialize it in a before stage routine. It works. Looking at your BussOrIndvClassify, do you really want the third line?

Code: Select all

return(Ans)
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

The other way is to rely on the fact that variables declared to be COMMON are automatically initialized to zero, therefore

Code: Select all

common /BussOrIndv/ a, aNumVals 

* only initialize if not already initialized

If a = 0
Then

* The second field in the array denotes where the substring 
* will occur: l = Left, r = Right, a = Anywhere in the str. 

   a = ""                                 ; aNumVals = 0

   a<-1> = "engineering" : @VM : "a"      ; aNumVals += 1
   a<-1> = "inc." : @VM : "r"             ; aNumVals += 1
   a<-1> = "limited" : @VM : "r"          ; aNumVals += 1
   a<-1> = "stores" : @VM : "a"           ; aNumVals += 1
   a<-1> = "consult" : @VM : "a"          ; aNumVals += 1
   etc... 

End
Avoiding the DCount function means one less pass through the dynamic array.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
mleroux
Participant
Posts: 81
Joined: Wed Jul 14, 2004 3:18 am
Location: Johannesburg, South Africa
Contact:

Post by mleroux »

My apologies, the second and third lines of BussOrIndvClassify

Code: Select all

Ans = aNumVals 
return(Ans)
are temporary debug insertions and shouldn't be there.

Thank you kindly for your suggestions, I'll give them a go. If I don't come right, "AYE'LL BEE BAAACK." :wink:
Morney le Roux

There are only 10 kinds of people: Those who understand binary and those who don't.
mleroux
Participant
Posts: 81
Joined: Wed Jul 14, 2004 3:18 am
Location: Johannesburg, South Africa
Contact:

Post by mleroux »

Schweet! Works well. Thanks to your fine help, there's only need for one routine:

BussOrIndvClassify

Code: Select all

common /BussOrIndv/ a, aNumVals

if a = 0
then
  gosub CreateArray
end

CleanEntityName = downcase(trim(EntityName))
for i = 1 to aNumVals
  begin case
    * If the occurrence may be anywhere in the string...
    case a<i,2> = "a"
      found = index(CleanEntityName, a<i,1>, 1)
    * If the occurrence must be at the left of the string...
    case a<i,2> = "l"
      found = index(left(CleanEntityName, len(a<i,1>)), a<i,1>, 1)
    * If the occurrence must be at the right of the string...
    case a<i,2> = "r"
      found = index(right(CleanEntityName, len(a<i,1>)), a<i,1>, 1)
  end case
  if found > 0
  then
    Ans = "B"
    exit
  end
next i

if found = 0
then
  Ans = "I"
end

return(Ans)

CreateArray:

* The second field in the array denotes where the substring
* will occur: l = Left, r = Right, a = Anywhere in the str.

a = "" 
a<-1> = "engineering" : @VM : "a" 
a<-1> = "inc." : @VM : "r" 
a<-1> = "limited" : @VM : "r" 
a<-1> = "stores" : @VM : "a" 
a<-1> = "consult" : @VM : "a" 
etc... 

aNumVals = dcount(a, @FM)

return(a)
One other question, though: if the array creation code is maintained, values being added, changed and deleted, how will the routine know to re-run the CreateArray gosub, i.e. how persistent is the common storage area? The DataStage BASIC manual says:

Code: Select all

A common area can be either named or unnamed. An unnamed common area is lost when the program completes its execution and control returns to the DataStage command level. A named common area remains available for as long as the user remains in the DataStage environment.
The user that runs the job is dsadm, so does that mean that the values stay for as long as the DataStage server is up, since dsadm runs the DataStage server processes, or is the persistence different to that?
Morney le Roux

There are only 10 kinds of people: Those who understand binary and those who don't.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

That's slightly misleading documentation; it should really say "the process" rather than "the user". Named COMMON is deallocated when the process finishes; in DataStage this means effectively when the active stage finishes, or when the job finishes if the COMMON is allocated in a before-job or after-job subroutine or in Job Control code.

So, next time you run the job, the array will be re-built as part of processing the first row.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
mleroux
Participant
Posts: 81
Joined: Wed Jul 14, 2004 3:18 am
Location: Johannesburg, South Africa
Contact:

Post by mleroux »

Ah, that makes sense. Thank you so much, Ray. Your assistance is always appreciated.
Morney le Roux

There are only 10 kinds of people: Those who understand binary and those who don't.
Post Reply