What does @_ -1 mean in Perl?

I am trying to translate a Perl script into PHP and I am having problems with some Perl stuff. For example, what does it mean @_ -1? And how do I write it in PHP?

The whole function is as follows:

sub variance {
    my $sum = sum_squares (@_);
    my $deg = @_ - 1;
    return $sum/$deg;
}

So, all the routines are as follows:

sub mean { # mean of values in an array
  my $sum = 0 ;
  foreach my $x (@_) {
    $sum += $x ;
  }
  return $sum/@_ ;
}

sub sum_squares { # sum of square differences from the mean
  my $mean = mean (@_) ;
  my $sum_squares = 0 ;
  foreach my $x (@_) {
    $sum_squares += ($x - $mean) ** 2 ;
  }
  return $sum_squares ;
}

sub variance { # variance of values in an array
  my $sum_squares = sum_squares (@_) ;
  my $deg_freedom = @_ - 1 ;
  return $sum_squares/$deg_freedom ;
}

sub median { # median of values in an array
  my @sorted = sort {$a <=> $b} (@_) ;
  if (1 == @sorted % 2) # Odd number of elements
    {return $sorted[($#sorted)/2]}
  else                   # Even number of elements
    {return ($sorted[($#sorted-1)/2]+$sorted[($#sorted+1)/2]) / 2}
}

sub histogram { # Counts of elements in an array
  my %histogram = () ;
  foreach my $value (@_) {$histogram{$value}++}
  return (%histogram) ;
}

Please carry me because he is my first time with Perl. From what I saw (tested), the correct answer in this case is David Doorwar. I have one more question regarding this set of routines, which is here .

+5
source share
7 answers

In this case @_, these are the arguments passed to the subprogram as a list.

.

, : variance('a', 'b', 'c', 'd');, $deg 3.

+9

, , . PHP

function variance() {
    $sum = sum_squares(func_get_args());
    $deg = func_num_args() - 1;
    return $sum/$deg;
}
// echo variance(1,2,3,4,5); // 13.75 (55/4)

sum_squares PHP . , , , . 2, ,

function sum_squares(array $args) {
    return array_reduce(
        $args, create_function('$x, $y', 'return $x+$y*$y;'), 0
    );
}
// echo sum_squares( array(1,2,3,4,5) ); // 55

create_function lambda PHP5.3

+7

, variance ( @_). @_ .

+2

@_ - sub, .

php - :

function variance() {
    $arguments = func_get_args();
    $sum = sum_squares($arguments);
    $deg = sizeof($arguments);
    return $sum/$deg;
}
+2

@_ - , paassed . , :

sum_squares (@_)

, (. ).

, $@.

, , ( , , sum_squares):

variance = arr[0]^2 + arr[1]^2 + ... + arr[N-1]^2
           --------------------------------------
                            N - 1

, .

, ( , - ). , , . , sum_squares , .

+1

, @_ Special Variable - Perl ( )

@_ , . . Perlsub.

+1

@_ , . , , . -1 @_

sub test 
{ 
my $count = @_ ; # Now it will assign the number of function arguments 
# Because we are trying to assign as a scalar. 
>print "Output:$count\n" ; # Output : 4 
($count ) = @_   # Now it will assign the first element of the functions 
print "Output:$count\n" ; #  OUtput: 10 
# Because we are trying to assign as list values , So ,list as only one variable  . 
# So , first element gets assigned. 
} 
&test ( 10,20,30,40 ) ; 
0

All Articles