PERL - Variable values getting mixed up!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL - Variable values getting mixed up!
# 1  
Old 06-10-2013
PERL - Variable values getting mixed up!

Hi,

I only dip my toe into PERL programming on the odd ocassion so I was wondering if anyone had any ideas as to why the below is happening:
When I run my PERL script the variable values seem to get mixed up.

Code:
my $fileName = basename($maxFile,".TXT");
my $currentSeqNum = substr($fileName,-1,1);
my $nextSeqNum = ($currentSeqNum)++;
print "File Name: $fileName\n\n";
print "Current Sequence Num: $currentSeqNum\n\n";
print "Next Sequence Num: $nextSeqNum\n\n";

Which produces:

Code:
MaxFile: FOT05172.TXT
File Name: FOT05172
Current Sequence Num: 3
Next Sequence Num: 2

$maxFile gets it value from an sql query using DBI which is correct. The problem lies in the fact that the "Current Sequence Num:" and "Next Sequence Num:" values seem to be the wrong way round.
Can anyone spot a mistake in my code?

Thanks
Chris
# 2  
Old 06-10-2013
Hi,

Issue is with the statement my $nextSeqNum = ($currentSeqNum)++;
It means first assign $currentSeqNum to $nextSeqNum and then increment $currentSeqNum.
Instead you could write like
Code:
 
my $nextSeqNum = $currentSeqNum;
print "Next Sequence Num: " ,++$nextSeqNum,"\n\n";

OR
Code:
 
my $nextSeqNum = $currentSeqNum + 1 ;
print "Next Sequence Num: $nextSeqNum\n\n";

~Pravin ~
# 3  
Old 06-10-2013
Good spot, option B is what I was after.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

2. Shell Programming and Scripting

Filtering values in variable

Hi, is there a faster/simpler way to filter values from the variable1 in variable2? example: variable1="A|B|C|E" variable2="A|B|C|D|F" output: "A|B|C" Thanks, zzavilz (4 Replies)
Discussion started by: zzavilz
4 Replies

3. Shell Programming and Scripting

Perl help - how to assign output of perl to variable

Hi, guys, i have a script i inherited from a coworker but i'm not perl savy. The script works but i would like it to work better. I want to run this command ./ciscomgrtest.pl -r "show version" -h hosts.router and have the script goto each router in the hosts.router file and run the command... (2 Replies)
Discussion started by: whipuras
2 Replies

4. Shell Programming and Scripting

Extract values from Perl variable

Hi Guys, I am stuck in a problem. I have a variable in Perl script which has value for example X=a-b-c; Now, I want to extract a b c separately into different 3 variables. I know this can be done in shell using awk but Perl behaves a bit different. Can anybody help me on this please?... (3 Replies)
Discussion started by: prashant2507198
3 Replies

5. UNIX for Dummies Questions & Answers

Variable is not substituting values

Hi All, OS HPUX 11.11 I am using following script to take controlfile backup. I have used SID variable to hold "ffin1" value, which I again subsitute in "'/db/ffin1/home/oraffin1/$SID_$wdate.ctl'" command. Well, after running this, SID variable does not subsittue it's value, while wdate... (6 Replies)
Discussion started by: alok.behria
6 Replies

6. Shell Programming and Scripting

substitute variable for values in perl

hi all, how do i assign values passed in from command line to and sql statement in perl ?? e.g i want to assign :name1 and :Name2 to be whatever is passed into the perl script command line my $sqlStr = "select * from test_table where column1 = upper(nvl(:name1, name1 )) and column2... (1 Reply)
Discussion started by: cesarNZ
1 Replies

7. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

8. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

9. Linux

How to store values into variable in perl

Hi, Can you please help me of how to store the values into variables. Here is the output in LINUX for the below command. $free output : total used free Mem: 3079276 3059328 19948 Swap: 1023992 6324 1017668 ... (3 Replies)
Discussion started by: chittiprasad15
3 Replies

10. Shell Programming and Scripting

getting values from variable in a loop

I have a set of variables: f1="./someFolder" . . f10="./someOtherFolder" And I'm trying to use the following loop for (( i = 0; i <= 10; i++ )) do temp=f$i done I'm trying the get the values from my set of variable to make directories, but I can't seem the get those value... (3 Replies)
Discussion started by: kriuz
3 Replies
Login or Register to Ask a Question