Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Report Design
Message Icon Topic: Array issue Post Reply Post New Topic
Author Message
Macavity
Groupie
Groupie


Joined: 24 Sep 2012
Online Status: Offline
Posts: 93
Quote Macavity Replybullet Topic: Array issue
    Posted: 03 Dec 2013 at 5:13am
Hi, I have an odd array issue

in a formula :
shared stringvar item;  (this is from a subreport)
shared stringvar array;
numbervar i1;

redim array[200];

array:= split(item,",")

for i1:= 1 to 200 do

if {table2.item} = array[i1] then do something

when running the report is keeps ending with error "a subscript must be between 1 and the size of the array"
On the left I see that i1 = 17

when I click check, it says "no errors"

What is going on here ?


IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 04 Dec 2013 at 4:49am
I am taking a guess, but the redim is before the split, and the split will automatically resize the array to just the amount needed.

How to work around, well, at least this is what I would do.

for i1:=1 to ubound(array)

oh, while we are at it, shouldn't the declaration of the array be:
shared stringvar array arr;
arr:=split(item)
for i1:=1 to ubound(arr)


just wondering, since it would seem that you are creating a stringvar called array, which I doubt is the intent.

HTH
IP IP Logged
Macavity
Groupie
Groupie


Joined: 24 Sep 2012
Online Status: Offline
Posts: 93
Quote Macavity Replybullet Posted: 09 Dec 2013 at 3:38am

Ubound was the solution, now I have another problem with arrays

 

Formula @test1 :

 

whileprintingrecords;

shared numbervar i1;

 

shared stringvar array item;

shared numbervar array price;

 

 

for i1:= 1 to ubound(item) do

 

if {tibom010_1.sitm} = item[i1] then

 

 price[i1] else 0;

 

this displays as a boolean :

 

true

 

formula @test2 :

 

shared numbervar array price;

 

price[1]

 

this displays correctly as a number 6916

 

why is @test1 returning a boolean, I tried with shared numbervar test1, but same result

IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 09 Dec 2013 at 4:57am
hmm...not sure. The syntax appears to be correct...just to check, I would try something like:

shared stringvar array item;
shared numbervar array price;
local numbervar x := 0;
for i1:= 1 to ubound(item) do
if {tibom010_1.sitm} = item[i1] then
    x:=price[i1];// else 0;

x


I am guessing that you want just 1 answer from the array, either the price or 0. This allows the formula to return just 1 value, as opposed to several that are mostly 0. I am guessing that this is what is confusing CR.

HTH
IP IP Logged
Macavity
Groupie
Groupie


Joined: 24 Sep 2012
Online Status: Offline
Posts: 93
Quote Macavity Replybullet Posted: 10 Dec 2013 at 4:36am
This is starting to haunt me, now I have a number, did it like you said, so that's ok, thanks !
But now, if I display the subreport I can see array item has : item1,item2,item3,item4
array price has 6916;70;73;40

on the main report it displays

blank  0
item2 70
item3 73
item4 40

the for i1:= 1 to ubound(item) seems to start from the second entry ????

hope you have idea
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 10 Dec 2013 at 4:55am
obviously, you can try starting at 0...but CR usually follows the VB6 syntax, which would be 1 based for arrays as opposed to the .Net syntax of being 0 based...

which leads to the next question, are you sure that array has the values...wait, you said that it does (subreport). does the subreport start at 1 as well. If so, that would seem to be ok...how about the sitm in the items array? Maybe the price is there but not the item?

After all the values are in the correct order and position, just the first item appears to be missing.

it's worth a try...

HTH
IP IP Logged
Macavity
Groupie
Groupie


Joined: 24 Sep 2012
Online Status: Offline
Posts: 93
Quote Macavity Replybullet Posted: 10 Dec 2013 at 7:04am
both first item and price are in the array.

if use the formulas
@test1
shared stringvar array item;

item[1]

@test2

shared numbervar array price;

price[1]

and show them on the main report, they display item1 and 6916 !

but not if I user i:= 1 to ubound(item)

I can't start at 0, cr doesn't accept it

IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 10 Dec 2013 at 7:19am
hmmm...
now it will get to the silly stuff...
are there leading or trailing spaces for item1?

it would seem that CR is not matching the value of item1...maybe another test would be:

@test1
shared stringvar array item;
"'" + item[1] + "'"

and/or the comparison should be:
if trim({tibom010_1.sitm}) = trim(item[i1]) then

worth a try
IP IP Logged
Macavity
Groupie
Groupie


Joined: 24 Sep 2012
Online Status: Offline
Posts: 93
Quote Macavity Replybullet Posted: 11 Dec 2013 at 9:15am
I'll try, but it's always the first item of the group that has price 0 while it is clearly present in the array

IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 11 Dec 2013 at 9:23am
for the @test1 you would expect:
'item1', but if you get something like 'item1 ' or 'item 1' then at least you have an idea of what is going wrong.

I completely believe that there is a value in the array, what I am not sure of is if the value is what you think it is....

you could also have a @test1.1
shared stringvar array item;
"'" + {tibom010_1.sitm} + "'"

this will check that the value you are comparing against is what you expect as well. maybe it is ITEM1, which won't match either.

just different ideas of how to see what CR is doing, instead of what we are imagining it is doing.

HTH
IP IP Logged
Post Reply Post New Topic
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



This page was generated in 0.031 seconds.