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);
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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s