1 Replies - 6980 Views - Last Post: 27 March 2013 - 03:44 PM

#1 deleteno5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-June 11

Why is my array printing twice?

Posted 27 March 2013 - 01:01 PM

I have three arrays in my simple code, and when I go to print the contents of the arrays, each one of them prints twice. Is it printing twice, or am I actually sticking two copies of elements when I push them (when all I want is to add one)? The relevant parts are here:

sub trim
{
	$name[0] =~ s/^\s+|\s+$//g;
	push(@name, $name[0]);
	print "@name\n";
	&obtainAge;
}



...and

 
	else
	{
		push(@age, $age[0]);
		print "@age\n";
		&obtainGpa;
	}



...and

	if($gpa[0] =~ m/^[+4].[+0-9]/)
	{
		push(@gpa, $gpa[0]);
		print "@gpa\n";		
		&obtainGo;
	}
	elsif($gpa[0] =~ m/^[+0-4].[+1-9]/)
	{
		push(@gpa, $gpa[0]);
		print "@gpa\n";	
		



I can plainly see by using the print statements you see that each array (@name, @age, @gpa) does in fact have two elements. For example, when ran, you are prompted for a name, then after you type one (and hit enter) it immediately prints TWO copies of the name you just entered. Same thing with age and GPA. What's going on here?

If you want to see the whole thing or run it, the full code is here:

#!/usr/bin/perl
use strict;
use warnings;

my $go;
my @name;
my @age;
my @gpa;
my $file;

&obtainGo;

sub obtainGo
{	
	print "Enter student data ('y' or 'n')? ";
	$go = <STDIN>;
	chomp $go;
	&evalGo($go);
}

sub evalGo
{
	if($go !~ m/y|n/)
	{
		print "Invalid response. Enter 'y' for yes or 'n' for no.\n";
		&obtainGo;
	}
	elsif($go eq 'y')
	{
		&obtainName;
	}
	elsif($go eq 'n')
	{
		print "Quitting.\n";
		print "@name \n";
		print "@age \n";
		print "@gpa \n";
	}
}

sub obtainName
{
	print "Name: ";
	$name[0] = <STDIN>;
	&trim($name[0]);
}

sub trim
{
	$name[0] =~ s/^\s+|\s+$//g;
	push(@name, $name[0]);
	print "@name\n";
	&obtainAge;
}

sub obtainAge
{
	print "Age: ";
	$age[0] = <STDIN>;
	chomp $age[0];
	&validateAge($age[0]);
}

sub validateAge
{
	if($age[0] !~ m/^\d+[0-9]$/)
	{
		print "Invalid age. Enter a positive, non-zero, two digit integer.\n";
		&obtainAge;
	}
	else
	{
		push(@age, $age[0]);
		print "@age\n";
		&obtainGpa;
	}
}

sub obtainGpa
{
	print "GPA: ";
	$gpa[0] = <STDIN>;
	chomp $gpa[0];
	&validateGpa($gpa[0]);
}

sub validateGpa
{
	if($gpa[0] =~ m/^[+4].[+0-9]/)
	{
		push(@gpa, $gpa[0]);
		print "@gpa\n";		
		&obtainGo;
	}
	elsif($gpa[0] =~ m/^[+0-4].[+1-9]/)
	{
		push(@gpa, $gpa[0]);
		print "@gpa\n";	
		&obtainGo;
	}
	else
	{
		print "Invalid input. GPA must have 1st digit 0-4, followed by ., then 0-9.\n0 Only allowed if 1st digit is 4.\n";
		&obtainGpa;
	}
}

sub obtainFile
{
	print "Enter file name: ";
	$file = <STDIN>;
	chomp $file;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why is my array printing twice?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Why is my array printing twice?

Posted 27 March 2013 - 03:44 PM

You do realize that you are printing each value as you collect the info and then print it again after quitting. Is that what you are talking about? Notice that in trim() you are printing the name, but you also print name if they quit.

Maybe that is the doubling you are seeing. Try commenting out the prints in your subs or comment out the prints at the end of quitting. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1