2008/3/13, Jenda Krynicky <[EMAIL PROTECTED]>:
>
> Date sent:              Thu, 13 Mar 2008 18:06:27 +0530
> From:                   "Sharan Basappa" <[EMAIL PROTECTED]>
> To:                     beginners@perl.org
> Subject:                functions: rotate and factorial
>
>
> > Hi,
> >
> > I was wondering if perl has support for the following operators or
> functions:
> >
> > - rotate: the elements of a given array are shifted such the elements
> > are shifted right or left
> > and the last/first element fill the first/last position depending on
> > whether shift right or shift left is
> > done.
>
>
> right
> @a = (pop(@a), @a);
>
> left
> @a = (@a[1..$#a], $a[0]);
>
> there's nothing preventing you from moving those to subroutines, eg.
> like this:
>
> sub shiftR (\@) {
>         my ($a) = @_;
>         @$a = (pop(@$a), @$a);
>         return;
> }
> sub shiftL (\@) {
>         my ($a) = @_;
>         @$a = (@{$a}[1..$#$a], $a->[0]);
>         return;
> }
>
> @a = (1,2,3,4);
> print join(',', @a), "\n";
>
> shiftR @a;
> print join(',', @a), "\n";
>
> shiftL @a;
> print join(',', @a), "\n";
>
> > - factorial
>
> Looks like it's for example in Math::NumberCruncher
>
> Jenda



if you don't know or want to use Math::Num.... or similar this may help

sub factorial {
        my $n = 1;
        $n *= $_ for 2..shift;
        return $n;
}

extracted from  http://timjoh.com/writing-a-factorial-subroutine-in-perl
I hop this  help somebody.

Reply via email to