By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,161 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,161 IT Pros & Developers. It's quick & easy.

Incorrect Format value return from the subroutine

P: 6
Perl, I have a main program which calls two subroutines (depending upon the report names).In the subroutine I am printing the data from CSV file using the format variable, Format_top prints the report header and STDOUT prints the report data. Both these subroutine runs fine individually, independent of each other. Now I have included both these subroutine in my main program and I call these subroutine depending upon the name of the report. Here is my problem:
1. When I run this program the format of second subroutine is displayed for the first subroutine.

2. Output display only the header and data for the first report and for the second subroutine it displays only data, no header is displayed for the second subroutine.

Can any one help me with this????
Apr 18 '07 #1
Share this Question
Share on Google+
10 Replies


KevinADC
Expert 2.5K+
P: 4,059
how can we help without seeing your code?
Apr 18 '07 #2

P: 6
Thanks for the responce, i am a beginner and not much familier with perl.
So here is my one subroutine and the second subroutine is also similar but the only difference is it has more number of columns.

Expand|Select|Wrap|Line Numbers
  1. sub abc() {
  2.     $last_element = @raw_data;
  3.     $report_name = @raw_data[0];
  4.     $column_name = @raw_data[1];
  5.  
  6.     chop($column_name);
  7.     $column_name =~s/"/\ /g;
  8.     ($column_name1, $column_name2, $column_name3, $column_name4) = split(/,/,$column_name);
  9.     write;
  10.  
  11.     foreach $field (@raw_data[2..$last_element]) {
  12.         chop($field);
  13.         $field=~tr/"//d;
  14.         ($Date, $code, $TotalMsg, $Zmsgs) = split(/\,/,$field);
  15.         write;
  16.     }
  17.  
  18.     format STDOUT_TOP =
  19.  
  20. @|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  21. $report_name
  22. @<<<<<<<<<<<@<<<<<<<@<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<
  23. $column_name1,$column_name2,$column_name3,$column_name4
  24. ==================================================================
  25. .
  26.  
  27.     format STDOUT =
  28. @<<<<<<<<<<<@<<<<<<<@<<<<<<<<<<@<<<<<<<<<<@
  29. $Date,$code,$TotalMsg,$Zmsgs
  30. .
  31.  
  32. }
  33.  
and in my main program i am calling these two subroutine as shown below, and i am passing the content of the CSV file as a parameter to the subroutine.

Expand|Select|Wrap|Line Numbers
  1. if (@raw_data[0] eq "Counts") {
  2.     &abc(@raw_data);
  3. } else (@raw_data[0] eq "Count1") {
  4.     &abc1(@raw_data);
  5. }
  6.  
I hope this makes things more clear.
Apr 19 '07 #3

P: 6
Can anybody please help with this!!!!
Apr 24 '07 #4

KevinADC
Expert 2.5K+
P: 4,059
I can't tell from what you posted what the problem might be. Post the code, with the second subroutine, and some sample data.
Apr 24 '07 #5

KevinADC
Expert 2.5K+
P: 4,059
actually, this is suspect:

Expand|Select|Wrap|Line Numbers
  1. if (@raw_data[0] eq "Counts")
  2. {
  3. &abc(@raw_data);
  4. }
  5. else (@raw_data[0] eq "Count1")
  6. {
  7. &abc1(@raw_data);
  8. }

the else {} block should not have any conditional statement:

Expand|Select|Wrap|Line Numbers
  1. if (@raw_data[0] eq "Counts")
  2. {
  3. &abc(@raw_data);
  4. }
  5. else 
  6. {
  7. &abc1(@raw_data);
  8. }
if you need to check the value of @raw_data[0] use an elsif condition:

Expand|Select|Wrap|Line Numbers
  1. if ($raw_data[0] eq "Counts")
  2. {
  3. &abc(@raw_data);
  4. }
  5. elsif ($raw_data[0] eq "Count1")
  6. {
  7. &abc1(@raw_data);
  8. else {
  9.    print "error";
  10. }
you should also be using $raw_data[0] instead of @raw_data[0]. $raw_data[0] is a scalar, @raw_data[0] isn't really a scalar but perl will probably treat it as one in that context.
Apr 24 '07 #6

P: 6
thanks for the responce and here is my second subroutine

Expand|Select|Wrap|Line Numbers
  1.  ##################################### 
  2. #Subroutine for Dealer reports
  3. #####################################
  4.  
  5. sub abc1()
  6. {
  7. local ($report_name,$column_name,$column_name1,$column_name2,$column_name3,$column_name4,$column_name5,$column_name6,$column_name7,$column_name8,$column_name9,$column_name10,$column_name11,$column_name12,$column_name13,$column_name14,$column_name15);
  8. local ($Date,$code,$Plan,$Beg,$Act,$NAct,$Add,$DeAct,$MIN,$Max,$Nadd,$EBal,$AUser,$TMsg,$Zm,$last_el)= 0;
  9.  
  10. $last_element=@raw_data; # to find the total number of elements in the file
  11.  
  12. $report_name = @raw_data[0];
  13. $column_name = @raw_data[1];
  14.  
  15. chop($column_name);
  16. $column_name =~s/"/\ /g;
  17. ($column_name1,$column_name2,$column_name3,$column_name4,$column_name5,$column_name6,$column_name7,$column_name8,$column_name9,$column_name10,$column_name11,$column_name12,$column_name13,$column_name14,$column_name15) = split(/,/,$column_name);
  18. write;
  19.  
  20.  
  21. foreach $field (@raw_data[2..$last_element])
  22. {
  23. chop($field);
  24. $field=~tr/"//d;
  25. ($Date,$code,$Plan,$Beg,$Act,$NAct,$Add,$DeAct,$MIN,$Max,$Nadd,$EBal,$AUser,$TMsg,$Zm,$last_el)=split(/\,/,$field);
  26. write;
  27. }
  28.  
  29. format STDOUT_TOP =
  30.  
  31. @|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  32. $report_name
  33. @<<<<<<<<<<<@<<<<<<<@<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<@<<<<<<<<<<@<<<<<<<<<<<<<@<<<<<<<<<<<<<@<<<<<<<<<<<<<<@<<<<<<<<<@<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<@<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<<<<<<
  34. $column_name1,$column_name2,$column_name3,$column_name4,$column_name5,$column_name6,$column_name7,$column_name8,$column_name9,$column_name10,$column_name11,$column_name12,$column_name13,$column_name14,$column_name15
  35. ==============================================================================================================================================================================================================================
  36. .
  37. format STDOUT =
  38. @<<<<<<<<<<<@<<<<<<<@<<<<<<<<<<@<<<<<<<<<<@<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<@<<<<<<<<<<@<<<<<<<<<<<<<@<<<<<<<<<<<<<@<<<<<<<<<<<<<<@<<<<<<<<<@<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<@<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<<<<<<<<
  39. $Date,$code,$Plan,$Beg,$Act,$NAct,$Add,$DeAct,$MIN,$Max,$Nadd,$EBal,$AUser,$TMsg,$Zm,$last_el
  40. .
  41. }
  42.  
From the main program i am sending the report header and that will avcivate the respective subroutine.
Apr 24 '07 #7

KevinADC
Expert 2.5K+
P: 4,059
Are you using a really old vesion of perl?
Apr 24 '07 #8

P: 6
Ok, so do u think that could be the the reason?
Can you please suggest me which version should i use to get read of this problem.
Apr 24 '07 #9

KevinADC
Expert 2.5K+
P: 4,059
if you are using an old version of perl I think the code would be OK. I never use the "write" function, so I really am at a loss to determine what the problem might be.

Whats with these gaps in the code you posted?

Expand|Select|Wrap|Line Numbers
  1. $column  _name4
  2. $MI  N
just a copy and paste problem?
Apr 24 '07 #10

P: 6
yes, i guess that a copy paste problem. Thanks for your help.
May 1 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.