جایی برای نوشتن

بایگانی
آخرین نظرات

۱۴ مطلب با موضوع «تکه کد پرل» ثبت شده است

۰۵آذر
#!/usr/local/bin/perl
use strict;
use warnings;

sub array_slice {
my $odd_or_even = shift @_;
my @arr;

my $i = $odd_or_even eq "even" ? 0 : 1;

while ($i <= $#_) {
$arr[@arr] = $_[$i];
$i += 2;
}

return @arr;
}

my @arr = array_slice "even", @ARGV;

print "Full array is: [@ARGV].\n";
print "Even position elements are: [@arr].\n";
...:::... محسن ...:::...
۰۵آذر
#!/usr/local/bin/perl
#
# search for an item in a numeric list.
# numeric list is privided by command line arguments.
# next we will read item to be searched from standard input.
#
use warnings;
use strict;

sub search {
my $toSearch = shift @_;
foreach my $i (@_) {
return 1 if ($i == $toSearch);
}
return 0;
}

my $progName = __FILE__;
if (!@ARGV) {
print STDERR "usage: $progName [num-list]\n";
exit 1;
}

print "list is [@ARGV]. Enter what to search?! ";
my $toSearch = <STDIN>;
chomp $toSearch;

my $result = search($toSearch, @ARGV) ? "in" : "not in";
print "$toSearch is $result [@ARGV]\n";
...:::... محسن ...:::...
۰۴آذر
#!/usr/local/bin/perl

#
# Reverse a list in place.
# list is provied as command
# line arguments.
#

use strict;
use warnings;

sub rev {
my $i = 0;
my $j = $#_;

while ($i < $j) {
@_[$i, $j] = @_[$j, $i];
$i++;
$j--;
}

return @_;
}

my @arr = rev @ARGV;
print "@arr\n";
...:::... محسن ...:::...
۰۴آذر
#!/usr/local/bin/perl

# A function that returns maximum
# number of a list. list provided
# from command line arguments.

use strict;
use warnings;

sub max {
my $m = shift @_;
foreach my $i (@_) {
if ($i > $m) {
$m = $i;
}
}
return $m;
}

print max(@ARGV), "\n";
...:::... محسن ...:::...