1

I want to read a string from a the first line in a file, then repeat it n repetitions in the console, where n is specified as the second line in the file.

Simple I think?

#!/usr/bin/perl
open(INPUT, "input.txt");
chomp($text = <INPUT>);
chomp($repetitions = <INPUT>);
print $text x $repetitions;

Where input.txt is as follows

Hello
3

I expected the output to be

HelloHelloHello

But words are new line separated despite that chomp is used.

Hello
Hello
Hello

You may try it on the following Perl fiddle CompileOnline

The strange thing is that if the code is as follows:

#!/usr/bin/perl
open(INPUT, "input.txt");
chomp($text = <INPUT>);
print $text x 3;

It will work fine and displays

HelloHelloHello

Am I misunderstanding something, or is it a problem with the online compiler?

Borodin
  • 126,100
  • 9
  • 70
  • 144
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82

1 Answers1

4

You have issues with line endings; chomp removes trailing char/string of $/ from $text and that can vary depending on platform. You can however choose to remove from string any trailing white space using regex,

open(my $INPUT, "<", "input.txt");
my $text = <$INPUT>;
my $repetitions = <$INPUT>;

s/\s+\z// for $text, $repetitions;
print $text x $repetitions;

I'm using an online Perl editor/compiler as mentioned in the initial post http://compileonline.com/execute_perl_online.php

The reason for your output is that string Hello\rHello\rHello\r is differently interpreted in html (\r like line break), while in console \r returns cursor to the beginning of the current line.

mpapec
  • 50,217
  • 8
  • 67
  • 127
  • So the problem is in the platform, not the code. Sounds interesting ! +1 for the regex. I'll mark up and solved ! Thanks – Ashraf Bashir May 03 '14 at 18:18
  • 2
    This doesn't explain why `print $text x 3` works, but `print $text x $repetitions` doesn't – Borodin May 03 '14 at 18:27
  • You are right @Borodin, this is still a pending question, so I'll remove the mark as answer flag until getting from the latter answers why it has this behavior. – Ashraf Bashir May 03 '14 at 19:47
  • @mpapec : what is `\z` ? – Baba May 03 '14 at 20:35
  • @mpapec: I don't understand what HTML has to do with this, but setting `$repetitions = "3\r"` produces the same result – Borodin May 03 '14 at 21:14
  • 1
    @Borodin I guess mpapec mentioned HTML because the asker using an online Perl compiler. – Lee Duhem May 04 '14 at 02:23
  • @AshrafBashir Try to run your script on a shell, you will see a different behavior. – Lee Duhem May 04 '14 at 02:25
  • @mpapec why this isn't reproduced with `print $text x 3` ? i.e. Why this line of code works fine in the online Perl compiler meanwhile `print $text x $repetitions` doesn't work well ? – Ashraf Bashir May 04 '14 at 21:19
  • @AshrafBashir it's not about `$repetitions` vs `3` that makes the difference, but `$text` which in your first example ends with `\r` control char, and in the second it doesn't. – mpapec May 04 '14 at 21:59
  • `print "has \\r\n" if $text =~ tr/\r//` to check whether $text contains special \r char. – mpapec May 04 '14 at 22:12
  • It doesn't have special \r, I tried your example `print "has \\r\n" if $text =~ tr/\r//` and it prints nothing – Ashraf Bashir May 05 '14 at 21:19
  • Your proposed reason doesn't explain why `print $text x 3` result is `HelloHelloHello` , while `print $text x $repetitions` result is `Hello Hello Hello` – Ashraf Bashir May 05 '14 at 21:21