Program to test with different basic functions in perl

Remove to comments to test different functions in perl.

#!/usr/bin/perl

# $substr = substr($string,$offset,$count)
# $substr = substr($string,$offset)
# substr($string,$offset,$count) = $newstring;  is used to replace the string with the new
# string
# substr($string,$offset) = $newstring;
# The number of A's in the unpack function is the number of variables towards left
# ($str1,$str2,$str3,$final) = unpack("A5 x3 A6 A8 A*",$data)
# split at five byte boundaries
# @fivers = unpack("A5" x (length($string)/5), $string);
# split individual characters
# @chars = unpack("A1" x length($string),$string);


# ---------------------------------------------------------------------------------

use Text::Tabs;


 sub cut2fmt {
	my(@positions) = @_;
	my $template = '';
	my $lastpos = 1;
	foreach $pos (@positions) {
		$template =  "A" . ($pos-$lastpos) . " ";
		$lastpos = $pos;
	}
	$template .= 'A*';
	return $template;
  }

$newstring = "is also a sample string";
$string = q{this is a sample string to be tested on different functions};
$string1 = q{this is a sample string to be tested on different functions};
$data = q{this is a sample string to test the unpack function};
$substr = substr($string,5,18);
$substr1 = substr($string,5);
print $substr . "\n";
print $substr1 . "\n";
substr($string1,5,18) = $newstring;
print $string1 . "\n";
$_ = $data;
$data = s/(\b)/ /;
print "$data";
($first,$last) = unpack("A4 A*", $data);
print $first . "\n";
print $last;


# -----------------------------------------------------------------------------------
# converting between ASCII characters and values
# this is to demonstrate and use function "ord" and "chr"
# Use ord to convert character to a number or use chr to convert number to character
# $char = sprintf("%c", $num); # slower than chr($num)
# printf("Number %d is character %c\n", $num, $num);

$num = 70;

$char = sprintf("%c", $num); # slower than chr($num)
print "$char" . "\n";
printf("Number %d is character %c\n", $num, $num);

# "unpack" and "pack" functions can also be used to convert many characters

$string = "test string\n";
@ASCII = unpack("C*", $string);
print "@ASCII";			#see the difference
print @ASCII;   		#see the difference
foreach $val (@ASCII) {
	print $val . " ";
	}
print "\n";
$STRING = pack("C*", @ASCII);
print "$STRING";



# -----------------------------------------------------------------------------------
# processing a string one character at a time
# and caluculating the checksum of a string

$string = "characters string";
%seen = ();
$sum = 0;

@array = split(//, $string);
print "@array\n";

@array1 = unpack("C*", $string);
foreach $num (@array1) {
	$sum += $num;
	printf("%c ", $num);
	}
print "\n";

while ($string =~ /(.)/g) {
	print "$1 ";
	$seen{$1}++;
	}
print "\nunique characters are ", sort(keys %seen), "\n";
print "the checksum of \"$string\" is $sum" . "\n";



# -----------------------------------------------------------------------------------
# reversing a string by word or character
# fucntions used are "reverse" , "join" and "split"

$string = "sample string to test these characters";
# $revstr = reverse($string);
 @words = split(" ", $string);
@revwor = join(" ", reverse @words);
print "@revwor";
# print "$revstr";


# -----------------------------------------------------------------------------------
# expanding and compressing tabs
# functions used are "expand" and "unexpand"
# use Text::Tabs module

$string = "sample    text   to test these characters\n";
while ($string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) { print "in the loop";}
print "$string";

$tabstop = 8;
# while() {print expand($_)}
# while() {print expand($_)}


# -----------------------------------------------------------------------------------
# expanding variables in user input

use vars qw($rows $coloums);
no strict 'refs';
my $text;
# print "enter number of rows :";
# chomp($rows = );
# print "enter number of coloums :";
# chomp($coloums = );
$text = q(i'm $rows high and $coloums long);
# $text =~ s/\$(\w+)/${$1}/g;
# $text =~ s/(\$\w+)/$1/gee;
# print $text;


# -----------------------------------------------------------------------------------
# interpolating functions

# $rec = "raghu:sample:pp:praveen:mukki";
# $wiw = "what i want is @{[join(" ",(split /:/, $rec))]} items";
# print $wiw;


# -----------------------------------------------------------------------------------
# trimming blanks from end of the string
# regexp : s/^\s+// to delete blanks from start of the string
# regexp : s/\s+$// to delete blanks and spaces from end of the string
# funtions used are trim and trim :D

$string = "      sample         test string to test trim nature            ";
$string =~ s/^\s+//;
$string =~ s/\s+$//g;
print $string;
$| = 0;
print "$%";
print "$=";
print "sampel";

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s