Back to Blog

VuGen: Extracting String Data From A Parameter

This post examines a condition where data being returned in the VuGen parameter captured (in the web_reg_save_param function) included more characters than was needed and there no easy left/right boundaries to use to get only what was needed. In this case, it was capturing something like “2297,4648”, and we needed to extract the first four numbers and the last four numbers of the string, while stripping out the “comma” character.

//We need to grab the 1st four (ENTERPRISEID)
//and the last four digits (which is a tax return )
//EXAMPLE: {pEnterpriseID_40} = "2297,4648"

// First, randomize based on total number of EnterpriseID's
srand( time(NULL) );
rnum1 = (rand () % atoi(lr_eval_string("{pEnterpriseID_count}")) +1);

//DEBUG ONLY LINE BELOW
//lr_output_message("INFO: The random number is %d", rnum1);

// Merge Parameter to include the random line number selected
 sprintf(tmpbuffer2,"{pEnterpriseID_%d}",rnum1 );

// Move lr paramter value of tmpbuffer2 to buffer2
 buffer2 = lr_eval_string(tmpbuffer2); 

// Clear Memory Contents Of variable
 sprintf(firstfour,"%s/0",firstfour);

// Clear Memory Contents Of variable
 sprintf(lastfour,"%s/0",lastfour); 

sprintf(firstfour,"%c%c%c%c",buffer2[0],buffer2[1],
     buffer2[2],buffer2[3]);
sprintf(lastfour,"%c%c%c%c",buffer2[5],buffer2[6],
     buffer2[7],buffer2[8]); 

// Convert The First And Last Four Numbers To A Parameter
 lr_save_string(firstfour,"pRndEnterpriseID");
 lr_save_string(lastfour,"pRndTaxReturn");

//Debug Code below. Use only if code above seems to not work
 //lr_output_message("INFO: EnterpriseID: %s = %s", buffer1,
 //  lr_eval_string("{pRndEnterpriseID}"));
 //lr_output_message("INFO: TaxReturn: %s = %s", buffer1,
 //  lr_eval_string("{pRndTaxReturn}"));
 //lr_output_message("buffer2 = %s, firstfour = %s,
 //  lastfour = %s",buffer2,firstfour,lastfour);

Below is what the output execution log might look like:

Action.c(104): Notify: Saving Parameter "pEnterpriseID_19 = 1595,1825"
Action.c(168): Notify: Parameter Substitution: parameter
     "pEnterpriseID_20" = "1595,1826"
 Action.c(175): Notify: Saving Parameter "pRndEnterpriseID = 1595"
 Action.c(176): Notify: Saving Parameter "pRndTaxReturn = 1826"

There are newer functions in LoadRunner these days which makes random selection a lot easier, but this is one way to do it.

Back to Blog