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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display array values using a for loop for line records

Status
Not open for further replies.

GC2000

Programmer
Sep 19, 2008
27
CA
Here's an interesting problem I am having in Crystal 9. Although I have found a resolution I am not convinced it is the most efficient way and I am struggling to understand why the loop would not work. I have a string that I put into an array that contains lines:

FiveSentence := split({Table.Worklog},Chr(10));

I need to pull the last 5 elements in the array. However I keep getting an error that the array must be between 1 and the size of the array. The data is:

This is Line 1
This is Line 2
<Null>
This is Line 3
This is Line 4

Here is the code I am using to troubleshoot because of the array error.

Local numberVar i := 0;
Local numberVar index := 0;
(
FiveSentence := split(Worklog,Chr(10));

for i := 1 to ubound(FiveSentence) do
index := i+1;
index & count(FiveSentence);
//FiveSentence[index];
);

I get index = 5 and count(FiveSentence)=4. So when I try to pull the FiveSentence[index] it errors out with the array error. Even if I set index to match the array count the loop only pulls the last value in the array instead of each instance. I have resolved the issue using if statements and concatenated the array contents so Array[1] & Array[2] etc...since it is only 5 sentences but again there must be something I am missing because I can't even get the loop to print an index value through each iteration. Any help would be much appreciated.
 
Should this

for i := 1 to ubound(FiveSentence) do

line be

for i = 1 to ubound(FiveSentence) do

In your code you are assigning i

Ian
 
When I change it from i:= to i = Crystal gives me an error that it is expecting an assignment.
 
Sorry.

YOu must assign i:= 1 at beginning, you currently have it as 0.

Ian
 
OK so in the variable declaration I have changed it to 1 but this does not change the output. It is still 5 and 4.
 
Please post your entire formula. You are not even showing the variable declaration for FiveSentence, and there are also some missing parens.

-LB
 
Thanks
Here is the whole code.

Local stringVar Worklog := {Table.Worklog};
Local stringVar array FiveSentence;
Local numberVar i := 1;
Local numberVar index := 0;

(
FiveSentence := split(Worklog,Chr(10));


for i := 1 to ubound(FiveSentence) do
index := i+1;
index & count(FiveSentence);
//FiveSentence[index];
);
 
But what is your goal here? If this loop is just for troubleshooting, what does the formula look like that you think should be pulling the last 5 elements in the array?

-LB
 
I see your point. Well if I could get past the whole iteration equaling the ubound of the array I would use;

fivesentence[index];
join (fivesentence,'');

Of course I would need to change the for loop to only count the last 5 iterations of the array. I guess what I am saying is that I can't really complete the code because I am not able to get past this point. All I want is the last 5 instances of the array to appear in the field.
 
stringvar y := {table.worklog};
stringvar array x := split(y,chr(10));
numbervar i;
numbervar j := ubound(x);
stringvar z;
for i := j-4 to j do(
if j >= 5 then
z := z + x else
z := replace(y,chr(10),"")
);
z

I set the default to the worklog when there were less than 5 elements, but removing the chr(10)--not sure what you wanted to do in that case. I also didn't add in a separator between the five elements. Again, not sure of your intent.

-LB
 
Well I tried it and it works perfectly. Thanks for all your help. From what I can see based on my original code I was trying to add complexity with another counter when it wasn't required.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top