user3591912
user3591912

Reputation: 9

New to Perl and was wondering why my code isn't doing what it's supposed to

I have an assignment asking me to print x iterations of a string for each character in that string. So if the string input is "Gum", then it should print out:

Gum  
Gum  
Gum

Right now my code is

my $string = <>;
my $length = length($string);
print ($string x $length, "\n");

And I'm getting gum printed five times as my output.

Upvotes: 1

Views: 73

Answers (4)

jackrabbit
jackrabbit

Reputation: 5663

As you are simply using the input string, it still contains the newline at the end. This is also counted as a character. On my system, it outputs 4 Gum\n.

chomp($string) will remove the line ending, but the output will then also run together, resulting in GumGumGum\n

Upvotes: 7

Borodin
Borodin

Reputation: 126722

Those who have said you will get CR + LF at the end of the line on a Windows system are mistaken. Perl will convert the native line ending to a simple newline \n on any platform.

You must bear this in mind whether you are reading from the terminal or from a file.

The built-in chomp function will remove the line terminator character from the end of a string variable. If the string doesn't end with a line terminator then it will have no effect.

So when you type GumEnter you are setting $string to "Gum\n", and length will show that it has four characters.

You are seeing it five times on your screen because the first line is what you typed in yourself. The following four are printed by the program.

After a chomp, $string is just "Gum" with a length of three characters, which is what you want.

To output this on separate lines you have to print a newline after each line, so you can write

my $string = <>;
chomp $string;
my $length = length $string;
print ("$string\n" x $length);

or perhaps

print $string, "\n" for 1 .. $length;

I hope that helps

Upvotes: 7

Chankey Pathak
Chankey Pathak

Reputation: 21666

Your code is working fine. See this: http://ideone.com/AsPFh3

Possibility 1: It might be that you're putting 2 spaces while giving input from command line, that's why the length comes out to be 5, and it prints 5 times. Something like this: http://ideone.com/fsvnrd

In above case the my $string=<>; will give you my $string = "gum "; so length will be 5.

Possibility 2: Another possibility is that if you use Windows then it will add carriage return (\r) and new line (due to enter \n) at the end of string. So it makes the length 5.

Edit: To print in new line: Use the below code.

#!/usr/bin/perl
# your code goes here
chomp(my $string=<>);
my $length = length($string);
print ("$string\n" x $length);

Demo

Edit 2: To remove \r\n use the below:

$string=~ s/\r|\n//g; Read more here.

Upvotes: 0

Krzysztof Wende
Krzysztof Wende

Reputation: 3247

When You insert input and press enter afterwards You don't enter "Gum" but "Gum\r\n" which is a string of length 5. You should do trimming.

Upvotes: 0

Related Questions