I am trying to teach myself perl and I cannot understand why when I print arrays from these subroutines that it prints an extra one that isnt there.

@name=fname(@name)."\n".lname(@name);


print @name;



sub fname(@name)
{
    @name=('john'."\t",'james'."\t",'edward'."\n");

print @name;
}

sub lname(@name)
{
    @name=('hunt'." \t",'jones'."\t",'smith');

 print @name; 
}

this is what I get when I execute it in the command prompt.

john     james     edward
hunt     jones      smith1
1

Now what I dont get is why the 1s on the end are there and how to remove them.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I am trying to teach myself perl and I cannot understand why when I print arrays from these subroutines that it prints an extra one that isnt there.

@name=fname(@name)."\n".lname(@name);


print @name;



sub fname(@name)
{
    @name=('john'."\t",'james'."\t",'edward'."\n");

print @name;
}

sub lname(@name)
{
    @name=('hunt'." \t",'jones'."\t",'smith');

 print @name; 
}

this is what I get when I execute it in the command prompt.

john     james     edward
hunt     jones      smith1
1

Now what I dont get is why the 1s on the end are there and how to remove them.

got it. You cant put calls to subroutines in print statements because it already executes them anyway.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

It is a best practice not to print but return.

@name=fname(@name)."\n".lname(@name);


print @name;

sub fname(@name)
{
@name=('john'."\t",'james'."\t",'edward'."\n");

return(@name);
}

sub lname(@name)
{
@name=('hunt'." \t",'jones'."\t",'smith');

return(@name);
}
This article has been dead for over six months. Start a new discussion instead.