Pages

Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Friday, April 20, 2012

read text file and store line in array



if (-e $file )
{
open(ori, $file);
while( my $line = )
{
if ($line =~ m/^connect/i)
{
push(@array,$line)
}
}
}
else
{
print" cannot find the file";
}


reference
1.http://stackoverflow.com/questions/1512729/how-can-i-search-multiple-files-for-a-string-in-perl

Thursday, April 12, 2012

verify whether array is empty perl

use strict; use warnings;
my @array;

if (@array)
{
print "array not empty";
}
else
{ print "array empty";
}
------------------------------------------
unless (@array)
{
print "There is element inside array.";
exit(1);
}

Wednesday, March 28, 2012

perl 2 array

my @num = qw (1 2 3 4 5); my @alp = qw (A B C D E); #One way for my $index (0 .. $#num) { print "$num[$index]-$alp[$index]\n"; } #Or print "$num[$_]-$alp[$_]\n" for (0 .. $#num);

Thursday, November 10, 2011

Array Perl



To get the index and item

for my $i (0 .. $#x) {
print "$i: $x[$i]\n";
}
To get the last item in array
. $array[$#array] .
To get the total number of array

.  scalar @array .
To get the max index in array
$#array
To Output the item of array
foreach (@array)
{
$element = $_;
print " $element\n"

}




References :
1. http://www.tutorialspoint.com/perl/perl_arrays.htm
2. http://perlmeme.org/howtos/data_structures/arrays.html

 

site weekly hits