BlueInk has asked for the wisdom of the Perl Monks concerning the following question:

First let me describe my program I Grab it From a Config File using this regex
if(/;;HPLCColumn;;([0-9]+);/){ if ($number == 2){ $HPLC_Collum= $1; $number ++; }else { die ; }
I then pass it to the function.  capconverter($excelfile ,$HPLC_to_UV_Factor ,$HPLC_Collum ,$UV_Collum ,$Data_row_start);
I then use it in the fuction as so
sub capconverter { my($excelfile ,$HPLC_to_UV_Factor ,$HPLC_Collum ,$UV_Collum ,$Data +_row_start)=@_; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $Book = $Excel->Workbooks->Open($excelfile); $Excel->{DisplayAlerts}=0; my $WorkBooknumber = 6; my $Sheet = $Book->Worksheets($WorkBooknumber); # your work sheet numb +er my $rowholder = $Data_row_start; while ((defined ($Sheet->Cells($rowholder,1)->{'Value'}))){#&&( define +d ($Sheet->Cells($rowholder,$HPLC_Collum)->{'Value'}))){ $Sheet->Cells($rowholder, 19)->{'Value'}= ($Sheet->Cells($rowholder,$H +PLC_Collum)->{'Value'} *$HPLC_to_UV_Factor); #to UV Calculation $rowholder ++; } $Book->SaveAs($excelfile); $Book->Close; }
Which gives me the following error
Win32::OLE(0.1709) error 0x800a03ec in METHOD/PROPERTYGET "Cells" at C:\Documents and Settings\KHusban +d\Desktop\ New Folder\exceel.pl line 272
How ever if i switch the code from $Sheet->Cells($rowholder,$HPLC_Collum)->{'Value'} to ($Sheet->Cells($rowholder,18)->{'Value'} It works.
My question is why is this and how can i get the value to be passed in threw the function. When i print  $HPLC_Collum it return the value of 18 which is expected.
--Thanks
BlueInk

Replies are listed 'Best First'.
Re: Cannot seem to pass number to a function
by moritz (Cardinal) on Aug 12, 2009 at 14:52 UTC
    Could you please throw out all the irrelevant stuff (like calls to Win32::OLE) and construct a small, runnable example that demonstrates your problem?

    $HPLC_Collum seems not to contain the number 18, you don't need Win32::OLE to find that out.

    Also please Use strict and warnings.

Re: Cannot seem to pass number to a function
by SuicideJunkie (Vicar) on Aug 12, 2009 at 14:54 UTC
    Have you tried printing the value of '$HPLC_Collum' before you use it as a parameter? Is it even getting set?

    You have not shown enough about where the value is coming from. It looks like you need a loop up there. Try tracking the state of your variable back to its source with lots of print statements to see where in the unshown code things go wrong.

    PS: be sure to use strict and warnings. When you spell variable names like 'collum', you're bound to run into trouble sooner or later, and those will help catch it.

      yeah i can track to print a value of 18 as seen in this quick example
      #!c:\perl\bin\ -w use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); use strict; my $configfile = "OleoresinSpreadsheet2.conf"; open (CONFIG,$configfile); my $HPLC_Collum; if(<CONFIG>=~/;;HPLCColumn;;([0-9]+);/){ $HPLC_Collum= $1; } my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $excelfile = 'F:\Spreadsheet\lab results\COPY GRID MASTER COPY.xls' +; my $Book = $Excel->Workbooks->Open($excelfile); my $WorkBooknumber = 6; my $Sheet = $Book->Worksheets($WorkBooknumber); print($HPLC_Collum); print($Sheet->Cells(6,$HPLC_Collum)->{'Value'});

      which leaves me with the following output
      Win32::OLE(0.1709) error 0x800a03ec in METHOD/PROPERTYGET "Cells" at C:\Documents and Settings\KHusban +d\Desktop\ New Folder\test2.pl line 20 Can't use an undefined value as a HASH reference at C:\Documents and S +ettings\KH usband\Desktop\New Folder\test2.pl line 20, <CONFIG> line 1. 18

      also the problem solves it self if i manualy assign the value such as
      $HPLC_Collum = 18
      --- Thanks
      BlueInk

        The Regex test in your second presentation is not the same as in your original post.

        In your original post you used the regex expression:

        if(/;;HPLCColumn;;([0-9]+);/){ if ($number == 2){ $HPLC_Collum= $1; $number ++; }else { die ; }

        Whereas in your response above you use:

        if(<CONFIG>=~/;;HPLCColumn;;([0-9]+);/){ $HPLC_Collum= $1; }

        Those are not at all the same. Hence, I don't think you're confirming what you think/say you're confirming by the latter example.

        In the former (i.e., the original) case you only set $HPLC_Colum to result of the 1st capture group (i.e., $1) if the value of $number matches the number 2. In the latter case (i.e., in your response) you set the value of $HPLC_Colum unconditionally to the value of $1 from your regex.

        So it looks to me like it is reasonable that you're getting the right answer from the latter but since I can see nowwhere in your original code where you set $number then either $HPLC_Colum never gets set or, presuming it is some global that gets set elsewhere in your code, then I can't tell if you ever get to set $HPLC_Colum.

        Of course, in that original form, if $number doesn't match the number 2 then you would execute the die which I would presume would be obvious. So it looks like $number is set to 2 elsewhere but your regex is not matching so you don't get your match group set like you're expecting.

        I concur with the other responders that you need to be using use strict; and use warnings; to make tracking down these kinds of problems easier. Also, as they suggested, getting familiar with and employing judicious diagnostic print statements to confirm that your variables are getting properly set is most helpful. I would also add that getting familiar with and judiciously using Perl's Debugger can also be helpful...and sometimes more convenient and straight-forward than messing with diagnostic print statements.

        I get these types of challenges frequently and have developed a pretty standard diagnostic strategy to track down the sources of the problems (the strategy is usually a combination of judicuous use of diagnostic prints and the debugger).

        ack Albuquerque, NM

        Maybe $Sheet->Cells() doesn't accept a string but does accept an integer. You could try $Sheet->Cells(6,0+$HPLC_Collum) to test this.