Tag Archives: Perl Scripts

Multidimesional array in perl…

We can write multidimensional array in Perl as follows :

    my @array1 = (1, 2, 3, 'four');
    my $reference1 = \@array1;
    my @array2 = ('one', 'two', 'three', 4);
    my $reference2 = \@array2;

    my @array = (\@array1, \@array2);

    print $array[1]->[0];

The output will be as follows :

one

How to generate random number in perl using rand()?

Using the Perl rand() function

Introduction

The rand() function is used to generate random numbers. By default it generates a number between 0 and 1, however you can pass it a maximum and it will generate numbers between 0 and that number.

Example 1. Between 0 and 1

To generate a random decimal number between 0 and 1, use rand() without any parameters.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $random_number = rand();

  print $random_number . "\n";

This will give you a random number, like:

  0.521563085335405

Example 2. A bigger range of numbers

Quite often you don’t want a number between 0 and 1, but you want a bigger range of numbers. If you pass rand() a maximum, it will return a decimal number between 0 and that number. Our example below generates a random number between 0 and 100.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $range = 100;

  my $random_number = rand($range);

  print $random_number . "\n";

The program will produce something like:

  34.0500569277541

Example 3. A random integer

To generate a random integer, convert the output from rand to an integer, as follows:

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $range = 100;

  my $random_number = int(rand($range));

  print $random_number . "\n";

This program gives you an integer from 0 to 99 inclusive:

  68

Example 4. With an offset

To generate a random number between, for example, 100 and 150, simply work out the range and add the minimum value to your random number.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $range = 50;
  my $minimum = 100;

  my $random_number = int(rand($range)) + $minimum;

  print $random_number . "\n";

This program gives you:

  129

How to generate unix time stamp in perl?

It can be done using :-

print time();

O/p

1387202679

How to redirect a page to another in perl?

It can be done as follows :

use CGI;
my $q = new CGI;
$CGI::HEADERS_ONCE = 1;
print $q->redirect(qq{newpage.html});

The thing we should care about this is we should not print anything in this page before the redirect code.
If anything prints here, then the redirect won’t work.

How to remove newlines or line separators in perl?

Using the Perl chomp() function

Introduction

The chomp() function will remove (usually) any newline character from the end of a string. The reason we say usually is that it actually removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a newline.

For more information on the $/ variable, try perldoc perlvar and see the entry for $/.

Example 1. Chomping a string

Most often you will use chomp() when reading data from a file or from a user. When reading user input from the standard input stream (STDIN) for instance, you get a newline character with each line of data. chomp() is really useful in this case because you do not need to write a regular expression and you do not need to worry about it removing needed characters.

When running the example below, using the enter key on a line by itself will exit the program.

  #!/usr/bin/perl
  use strict;
  use warnings;

  while (my $text = <STDIN>) {
    chomp($text);
    print "You entered '$text'\n";
    last if ($text eq '');
  }

Example usage and output of this program is:

  a word
  You entered 'a word'
  some text
  You entered 'some text'

  You entered ''

Example 2. Chomping an array

If you chomp an array, it will remove a newline from the end of every element in the array:

  #!/usr/bin/perl
  use strict;
  use warnings;

  my @array = ("bob\n", "jill", "fred\n");

  print "Before chomp:\n";
  print "@array\n";

  chomp(@array);

  print "After chomp:\n";
  print "@array\n";

This program produces the following output:

  Before chomp:
  bob
   jill fred

  After chomp:
  bob jill fred

As you can see, the newlines have been removed from “bob” and “fred”, but no characters have been removed from “jill”.

Example 3. Chomping a hash

If you pass a hash into chomp(), it will remove newlines from every value (not key) of the hash:

  #!/usr/bin/perl
  use strict;
  use warnings;

  my %hash = (
    'first' => "one\n",
    'second' => "two\n",
    'third' => "three\n",
  );

  chomp(%hash);

  foreach my $k (keys %hash) {
    print "$k: $hash{$k} ";
  }

  print "\n";

  exit 0;

This program outputs:

  first: one second: two third: three