We have moved to www.dataGenX.net, Keep Learning with us.

Monday, October 01, 2012

Useful Perl Scripts : Part-2



1.   To Split the text and join


#!/usr/bin/perl


$lin = "Hello who is there in this house";
# my @parts = <$_>;
my @parts = split(/\s+/,$lin);
$string = join("  ",@parts[0..3]);
$string1 = join(" ",@parts[4..6]);
print "String is $string\n";
print “String1 is $string1\n”;

Sample Output:

String is Hello who is there
String1 is in this house

 

 

2.   To Fetch a substring of a string


#!/usr/bin/perl

$string = "hello!! Are you there!!";
$temp = substr($string,2);          # starts from the 2nd position of a string.
$temp1 = substr($string,0,2);             # starts at 0'th position of a string and ends at 2nd
print "Substring value is $temp\n";       # Substring value is  llo!! Are you there!!
print "substring value is $temp1\n";      # Substring value is  he

 

 

3.   To Find length of an Array


#!/usr/bin/perl

@array = (1,2,3,4);
print $#array+1;






njoy the simplicity.......
Atul Singh

No comments :

Post a Comment