Perl read file into a variable

— Read the contents into a scalar

The whole file is stored in a single scalar variable. To do this, the special variable $/ should have an undefined value when reading the file.

Here’s one way to do it:

open FILE, "<file.txt";
$file_contents = do { local $/; <FILE> };

When you need to process small files, it’s usually easier to read the whole file contents in a variable; here’s how to do it:

— Read the contents into an array

Each row will be stored in an array element:

open FILE, "<file.txt";
@lines = <FILE>;

Advertisements

Perl program implementing a stack inteface for integers

The interface should have 'push' and 'pop' functions. You will be asked to 'push' a series of integers and then 'pop' and print out every alternate integer.
Input Sample:
1,2,3,4,5,6,7,8,-9
Output:
1,3,5,7,-9
Script:

open(INFILE, "$ARGV[0]") or die("Cannot open file $_[0] for reading: $!");
while (my $line = <INFILE>){
    chomp($line);
	my $count = 0;
	my $i;
	my @array;
	my @input = split(/ /,$line);
	my $length = scalar(@input);
	while($length > 0){
		$i = pop(@input);
		if($count eq 0){
			print "$i ";
			$count = 1;
		}else{
			$count = 0;
		}
		$length--;

	}
		print "\n";
}
close(INFILE);

Determine if a string is a rotation a given string using Perl

Input Sample:

Hello, lloHe

Output Sample:

true

Script:

open(INFILE, "$ARGV[0]") or die("Cannot open file $_[0] for reading: $!");

    while(my $line = <INFILE>) {
	chomp($line);
	my $i=0,$position,$count=0,$j,$flag = 1;
	my @arg = split(/,/,$line);
	my @word1 = split(//,$arg[0]);
	my @word2 = split(//,$arg[1]);
	my $len1 = scalar(@word1) - 1;
	my $len2 = scalar(@word2) - 1;
	if($len1 != $len2){
		print "False\n";
		next;
	}

	foreach(@word2){
		if($_ eq $word1[0]){
			$position = $count;
			last;
		}
		$count++;
	}

	for($i=0;$i <= $len1;$i++){
		if($position > $len2){
			$position = 0;
		}
		if($word1[$i] eq $word2[$position]){
			$position++;
			next;
		}else{
			print "False\n";
			$flag = 0;
			last;
		}
	}
	if($flag eq 1){
		print "True\n";
	}

}

close(INFILE);

Perl Script to swaps letters’ case in a sentence.

Input Sample:

Ishaan is THE BEst

Output Sample:

iSHAAN IS the beST

Script:

open(INFILE, "$ARGV[0]") or die("Cannot open file $_[0] for reading: $!");

	while(my $line = <INFILE>) {
	chomp($line);
	my @argument = split(/ /,$line);
	foreach(@argument){
		my @word = split(//,$_);
		foreach(@word){

			if($_ eq uc($_)){

				print lc($_);
			}else{
				print uc($_);
			}
		}
		print " ";

	}
	print "\n";

	}
	close(INFILE);

Using the Minimum coins to arrive at a total using Perl

Input Sample:

Your program should accept as its first argument a path to a filename. Each line in this file contains a positive integer number which represents the total you have to arrive at e.g.

11
20

Script:
open(INFILE, "$ARGV[0]") or die("Cannot open file $ARGV[0] for reading: $!");
 while(my $line = <INFILE>) 
{ chomp($line); 
my $num = $line; 
my $snum; 
my $sum = 0; 
$sum = $sum + int($num/5); 
$num = $num%5;
 $sum = $sum + int($num/3); 
$num = $num%3; 
$sum = $sum + int($num/1); 
$num = $num%1;
 print "$sum\n";
 } 
close(INFILE);