# Thread: How to get last 4 digits in an array?

1. ## How to get last 4 digits in an array?

Hello all! This is probably a simple question, but I am new to working with arrays in WinRunner. The TSL online help was unable to help me with this problem.

I need to create a snippet of code which takes a number, performs a calculation, then <font color="red">returns the last 4 digits of the answer. </font> Any ideas? I was thinking I could assign the result to an array and then return the last 4 digits, but I am not sure how to do this with TSL.

------------------------------------
authcode = ((myNumber*357) - 172);
arr2[] = {authcode};

# assign last 4 digits of authcode to new variable

------------------------------------

Any ideas?

2. ## Re: How to get last 4 digits in an array?

Would the SUBSTR function work?
substr (string, position [, length ] );

i.e.
ipos = (length(authcode)-4);
substr (newvar, ipos, 4 );

Not sure if this is what you are attempting to do.

3. ## Re: How to get last 4 digits in an array?

djparks - an array is not the answer to everything [img]/images/graemlins/wink.gif[/img]

It also seems that in addition to working with WinRunner, you are also new to coding. I would take a step back and work with your management to receive training/mentoring in programming logic and skills. If you are going to continue your chosen path with any automation tool, learning to program is paramount (and is usually a prerequisite before starting automation, not something that is being learned at the same time)

4. ## Re: How to get last 4 digits in an array?

[ QUOTE ]
Would the SUBSTR function work?
substr (string, position [, length ] );

i.e.
ipos = (length(authcode)-4);
substr (newvar, ipos, 4 );

Not sure if this is what you are attempting to do.

[/ QUOTE ]

Yes, that's pretty much what I was looking for. I actually came up with something simliar before your post.

Here is the completed end result:

--------------------------------------------------
public function get_authcode ()
{
static arr[];

str = text

set_window("pop_up", 1);
obj_get_text("Edit", text);

i = split(str, arr, " .");
myNumber = arr[i-1];

authcode = ((myNumber*357) - 172);

a = substr ( authcode, length(authcode)-3 ,4);

return a;
}
-----------------------------------------------------

5. ## Re: How to get last 4 digits in an array?

[ QUOTE ]
djparks - an array is not the answer to everything [img]/images/graemlins/wink.gif[/img]

It also seems that in addition to working with WinRunner, you are also new to coding. I would take a step back and work with your management to receive training/mentoring in programming logic and skills. If you are going to continue your chosen path with any automation tool, learning to program is paramount (and is usually a prerequisite before starting automation, not something that is being learned at the same time)

[/ QUOTE ]

This is true, I've worked software QA for 5 years now but I am new to automation. Also, aside from a few classes in college (I just graduated last year) I am pretty fresh to programming in general. I am picking up things as I go along, and for the most part have been very successful. The scripts I have been writing thus far have had a great deal of programming, but the functions themselves have been pretty simple.

In summary, I am definitely trying my best to learn, because I really enjoy what I do! Thanks for the advice!

6. ## Re: How to get last 4 digits in an array?

OK - then quit using arrays (except for a few cases) in WR. They are, as Tony says, not the answer to everything. They are ESPECIALLY not the answer to string manipulation.

7. ## Re: How to get last 4 digits in an array?

By the way - let me guess - you learned C in college, right?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.