Tutorialspoint

Perl nested Loop

#/usr/local/bin/perl
   
$a = 0;
$b = 0;

# outer while loop
while($a < 3) {
   $b = 0;
   # inner while loop
   while( $b < 3 ) {
      print "value of a = $a, b = $b\n";
      $b = $b + 1;
   }
   $a = $a + 1;
   print "Value of a = $a\n\n";
}

atan2 function example

#!/usr/bin/perl

$Y = 30;
$X = 60;

$ret_val = atan2 ($Y, $X );

print "atan2 of 30/60 is : $ret_val\n";

Matching Only Once

#!/usr/bin/perl

@list = qw/food foosball subeo footnote terfoot canic footbrdige/;

foreach (@list) {
   $first = $1 if /(foo.*?)/;
   $last = $1 if /(foo.*)/;
}
print "First: $first, Last: $last\n";

$[ Special Variable

#!/usr/bin/perl

# define an array
@foods = qw(pizza steak chicken burgers);
print "Foods: @foods\n";

# Let's reset first index of all the arrays.
$[ = 1;

print "Food at \@foods[1]: $foods[1]\n";
print "Food at \@foods[2]: $foods[2]\n";

Perl last Function

#!/usr/bin/perl

$count = 0;

while( 1 ) {
   $count = $count + 1;
   if( $count > 4 ) {
      print "Going to exist out of the loop\n";
      last;
   } else {
      print "Count is $count\n";
   }
}
print "Out of the loop\n";

Perl y Function

#!/usr/bin/perl -w

$string = 'the cat sat on the mat.';

# This will generate a upper case string
$string =~ y/a-z/A-Z/;

print "$string\n";

Perl warn Function

#!/usr/bin/perl -w

warn("Unable to calculate value, using defaults instead.\n");

Perl wantarray Function

#!/usr/bin/perl -w

sub foo {
   return(wantarray() ? qw(A, B, C) : '1');
}

$result = foo();    # scalar context
@result = foo();    # array context

print("foo() in a  scalar context: $result\n");
print("foo() in an array  context:
@result\n");

Perl vec Function

#!/usr/bin/perl -w

$vec = '';
vec($vec,  3, 4) = 1;  # bits 0 to 3
vec($vec,  7, 4) = 10; # bits 4 to 7
vec($vec, 11, 4) = 3;  # bits 8 to 11
vec($vec, 15, 4) = 15; # bits 12 to 15
# As there are 4 bits per number this can
# be decoded by unpack() as a hex number
print("vec() Has a created a string of nybbles, in hex: ", unpack("h*", $vec), "\n");

Perl values Function

#!/usr/bin/perl -w

%hash = ('One' => 1,
         'Two' => 2,
         'Three' => 3,
         'Four' => 4);

@values = values( %hash );
print("Values are  ", join("-", @values), "\n");

@keys = keys( %hash );
print("Keys are ", join("-", @keys), "\n");

1 2 3 4 5 6 7 ... 23 Next
Advertisements
Loading...