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

Perl Variable in Variables?????

Status
Not open for further replies.

jollyroger

Technical User
Mar 22, 2001
77
0
0
GB
Hello there, thanks to anyone who helps me here. Here is my bit of the program:
print LOG "Starting the Parameter checks and changes\n";
$i = 1;
while ( $i lt 9 ) {
print LOG "The rolling variable is $i\n";
if ( ${SQLPAR}$i =~ TODAY ) {
${SQLPAR}$i = $dateora;
print LOG "SQLPAR$i: ${SQLPAR}$i\n";
} elsif ( $SQLPAR$i =~ FIRST ) {
$SQLPAR$i = $FIRST_DATE;
print LOG "SQLPAR$i: $SQLPAR$i\n";
} elsif ( $SQLPAR$i =~ LAST ) {
$SQLPAR$i = $LAST_DATE;
print LOG "SQLPAR$i: $SQLPAR$i\n";
$i++;
} else {
print LOG "No Date Values\n";
}
}
OK, currently I have the Variables $SQLPAR1, $SQLPAR2 etc up to 8. I however need to check values against these variable such that if $SQLPAR4 =~ FIRST then it will assume the first date of the month, cool. But how to I write this in short form, so that if I need to increase my list of variable I can simply change the 9 to a 12.
Also this will not work, I think because I don't know how to enter the variable $SQLPAR$i so that the system recognises that I am saying $SQLPAR1 etc.
I hope you understand this from my program etc
Best regards
Ben
 
Create your variables like this:

$SQLPAR = 'SQLPAR' . $i.

$$SQLPAR = $dataora;

 
Dam that was quick!!
I was just about to answer to suggest that I use the foreach loop, but this also seems not to work, because it does not take the values I am then re-assigning further on in the script.
You you are saying that I should create the variable in a different way, but actually the variable are read in from a file. If the parameters are extra variable which need extra work (e.g. date values from month to month) then they need to change from the actual value to the "date" value.
I don't see how creating the variable like this will help, because it needs to over write the previous value, and I am not sure it will do this.
Anyway I am going home now and will test later.
Thanks
 
If you're storing data in variables ending in numbers and you're trying to loop over them, why not just store it in an array instead? Instead of [tt]$var0 = 'asdf'[/tt] say [tt]$var[0] = 'asdf'[/tt], then you can, without hassle, say [tt]$var[$i][/tt] and increment [tt]$i[/tt].

Also, when using a regular expression, you have to have a start/end delimiting character (commonly a forward slash). For instance, [tt]$var =~ FIRST[/tt] should be [tt]$var =~ /FIRST/[/tt]

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Hey there
Thanks for your help, I took some of it on board and then somemore, and this is what I have which now works!!!

print LOG "Starting the Parameter checks and changes\n";
@SQLPAR = ($SQLPAR1, $SQLPAR2, $SQLPAR3, $SQLPAR4, $SQLPAR5, $SQLPAR6, $SQLPAR7, $SQLPAR8);
$i = 0;
while ( $i lt 8 ) {
if ( $SQLPAR[$i] =~ /TODAY/ ) {
$SQLPAR[$i] = $dateora;
print LOG "SQLPAR$i: $SQLPAR[$i]\n";
} elsif ( $SQLPAR[$i] =~ /FIRST/ ) {
$SQLPAR[$i] = $FIRST_DATE;
print LOG "SQLPAR$i: $SQLPAR[$i]\n";
} elsif ( $SQLPAR[$i] =~ /LAST/ ) {
$SQLPAR[$i] = $LAST_DATE;
print LOG "SQLPAR$i: $SQLPAR[$i]\n";
$i++;
} else {
print LOG "No value set\n";
$i++;
}
}
print LOG "The values are now: @SQLPAR \n";

Thanks again
Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top