three set of inputs for one subroutine

K

king

Hi,

I have a subroutine as below.And I want to give three different set of
inputs to that. And I want to save the output which I got by feeding
three set of different inputs to the subroutine.

I want to use the same subroutine for the below inputs.
1st set: $1, $2, $3
2nd set: $4, $5, $6
3rd set: $7, $8, $9

#######################################

sub calc
{

my $BinaryBusString = sprintf "%08b", $1;
my $BinaryDevString = sprintf "%05b", $2;
my $BinaryFuncString = sprintf "%03b", $3;
my $temp=join('','0000',$BinaryBusString,$BinaryDevString,
$BinaryFuncString,'000000000000');

my $hex = '';

while ($temp =~ s/(\d{4})$//)
{
$hex = sprintf('%X', oct("0b$1")).$hex;
}
unless ($temp eq '')
{
$hex = sprintf('%X', oct("0b$temp")).$hex;
}
$hex=~s/0/E/i;


my $Resv_Process_Bridge=$hex;
my $count_Bridge=1;
my $Resv_Bridge=40;
my $output;
my $last_string;
& nvpci;

sub nvpci
{
$Resv_Process_Bridge=~s/..$/$Resv_Bridge/i;
$output=`gsdk pd $Resv_Process_Bridge`;
$last_string = substr("$output", 72,2);
}

while ($last_string!="10")
{
$count_Bridge = $count_Bridge+1;
if ($count_Bridge == 15)
{
print "count is: $count_Bridge and may be a wrong input is being
given\n";
last;
}
$Resv_Bridge= substr("$output", 70,2);
& nvpci;
}
if($count_Bridge < 10)
{
&Calcuate_Bridge_Register;
}
sub Calcuate_Bridge_Register
{
my $output_1=sprintf "%08X\n",oct("0x$Resv_Process_Bridge")
+oct("0x08");
my $output_2=sprintf "%08X\n",oct("0x$Resv_Process_Bridge")
+oct("0x10");
my $output_3=sprintf "%08X\n",oct("0x$hex")+oct("0xf00");
my $output_4=sprintf "%08X\n",oct("0x$hex")+oct("0x170");

}

}
#######################################################

I want to save all the $output_1, $output_2, $output_3 and $output_4
for all the three set of inputs.
Writing three different subroutine for three different set of inputs
is quite lengthy a process.

I am not getting the exact idea how to use the same subroutine to do
it.
 
J

Josef Moellers

king said:
Hi,

I have a subroutine as below.And I want to give three different set of
inputs to that. And I want to save the output which I got by feeding
three set of different inputs to the subroutine.

I want to use the same subroutine for the below inputs.
1st set: $1, $2, $3
2nd set: $4, $5, $6
3rd set: $7, $8, $9

#######################################

sub calc
{

my $BinaryBusString = sprintf "%08b", $1;
my $BinaryDevString = sprintf "%05b", $2;
my $BinaryFuncString = sprintf "%03b", $3;

[ ... ]
#######################################################

I want to save all the $output_1, $output_2, $output_3 and $output_4
for all the three set of inputs.
Writing three different subroutine for three different set of inputs
is quite lengthy a process.

I am not getting the exact idea how to use the same subroutine to do
it.

Just do what is usually done in programs: write a sub(routine) with
arguments:

sub calc {
my ($bus, $dev, $func) = @_;

my $BinaryBusString = sprintf "%08b", $bus;
my $BinaryDevString = sprintf "%05b", $dev;
my $BinaryFuncString = sprintf "%03b", $func;

...

As to multiple return values, you can return a list from a sub(routine):

return ($output_1, $output_2, $output_3, $output_4);
}

Note that you must then store the result either in a list of variables
or in an array:

my @result = calc($input_bus, $input_device, $input_function);
or
my ($r1, $r2, $r3, $r4) = calc($input_bus, $input_device, $input_function);

Josef
 
P

Peter Wyzl

king said:
Hi,

I have a subroutine as below.And I want to give three different set of
inputs to that. And I want to save the output which I got by feeding
three set of different inputs to the subroutine.

I want to use the same subroutine for the below inputs.
1st set: $1, $2, $3
2nd set: $4, $5, $6
3rd set: $7, $8, $9

#######################################


I would caution against really using $1, $2 etc since they are used in
capturing. But to demonstrate the syntax...

you call the sub by

calc($one, $two, $three); # more meaningful names would be helpful...

Those three values are passed into the sub via the @_ special array and can
be accessed a number of ways, by shifting them off the array into lexical
variables (preferred) by a slice of the @_ array (@_[0] ) or similar. So
inside the sub you would then write something like..


sub calc {

my ($one, $two, $three) = @_; # this gets all of them...

And then use them as normal.

next call of the sub would be

calc($three, $four, $five);

And those populate the $one, $two, $three variables in the sub for that
iteration. Hence better names would be appropriate... something that
indicates what type of data that value represents.
{
my $output_1=sprintf "%08X\n",oct("0x$Resv_Process_Bridge")
+oct("0x08");
my $output_2=sprintf "%08X\n",oct("0x$Resv_Process_Bridge")
+oct("0x10");
my $output_3=sprintf "%08X\n",oct("0x$hex")+oct("0xf00");
my $output_4=sprintf "%08X\n",oct("0x$hex")+oct("0x170");

}

}
#######################################################

I want to save all the $output_1, $output_2, $output_3 and $output_4
for all the three set of inputs.
Writing three different subroutine for three different set of inputs
is quite lengthy a process.

The subroutine will return either the last value evaluated, or a list passed
to the 'return' function, so your last line in the subroutine may look
something like...
(contexct of the call matters)

return($output_1, $output_2, $output_3, $output_4);

This returns a LIST made from those 4 scalars to the call... so to capture
those values, your call needs to be somthing like...

my ($out1, $out2, $out3, $out4) = calc($one, $two, $three);

Then do something similar for the next pass of the sub...

Read perldoc perlsub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top