-1

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 by mike_2000_17: Fixed formatting

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by dweebsonduty
0

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 by mike_2000_17: Fixed formatting

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.