three set of inputs for one subroutine

three set of inputs for one subroutine

am 07.11.2007 14:42:23 von 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.

Re: three set of inputs for one subroutine

am 07.11.2007 15:01:21 von Josef Moellers

king wrote:
> Hi,
>=20
> 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.
>=20
> 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
>=20
> #######################################
>=20
> sub calc
> {
>=20
> my $BinaryBusString =3D sprintf "%08b", $1;
> my $BinaryDevString =3D sprintf "%05b", $2;
> my $BinaryFuncString =3D sprintf "%03b", $3;

[ ... ]

> #######################################################
>=20
> 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.
>=20
> 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=20
arguments:

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

my $BinaryBusString =3D sprintf "%08b", $bus;
my $BinaryDevString =3D sprintf "%05b", $dev;
my $BinaryFuncString =3D 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=20
or in an array:

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

Josef
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

Re: three set of inputs for one subroutine

am 07.11.2007 15:05:32 von Peter Wyzl

"king" wrote in message
news:1194442943.106195.197520@v23g2000prn.googlegroups.com.. .
> 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

--
P



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