Showing posts with label Undefined variable. Show all posts
Showing posts with label Undefined variable. Show all posts

Jun 28, 2013

Delete element from Array in Perl

You can use 'delete' command for deleting an element from an array.
When you delete an element from the middle of an array, that particular elements is undef (ie., not defined)

Please refer to the below program:

#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my @array = qw/10 20 30 40 50/;

print "\n Before Deleting :" . Dumper(\@array);

delete $array[2];

print "\n After Deleting :" . Dumper(\@array);
  

Output:

Before Deleting :$VAR1 = [
          '10',
          '20',
          '30',
          '40',
          '50'
        ];

 After Deleting :$VAR1 = [
          '10',
          '20',
          undef,
          '40',
          '50'
        ];
  


Jun 22, 2008

Defining a undefined variable

If a variable is not defined, we can define like this instead of IF block.

#!F:\Perl\bin\perl
use strict;
use warnings;

# Very simple and easy to use
$a;
$a |= "prabhath";
print "\n Value is:", $a;


=cut
We can avoid the unnecessary if and defined code

$a = 'vamsi';
if ( not defined $a) {
$a = 'prabhath';
}
=cut