perlnoobster has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have the following coding but cannot for the life of me figure out how to Chomp the $colr variable (it's creating a new blank line below the string):
use strict; use warnings; use Readonly; my $colr=(); my $output = "phones_out.txt"; my $counter = 0; open(OUT,"+>$output"); Readonly::Scalar my $PHONE_FILE => $ARGV[0] || 'phones.txt'; Readonly::Array my @COLOURS => <DATA>; Readonly::Scalar my $GET_MODEL => qr{\A ( [^\n]+ ) }x; open my $PHONES, '<', $PHONE_FILE or die "Cannot open $PHONE_FILE\n"; while (my ($phn) = <$PHONES> =~ $GET_MODEL) { no warnings 'uninitialized'; foreach my $colr (@COLOURS) { $counter++; # chomp $colr; print OUT "\tEAN\thello $phn $colr \n"; } } close $PHONES __DATA__ baby blue baby pink black dark blue brown dark purple green orange hot pink light purple red white yellow
the phones.txt file contains the following (in one column, below one another):
Apple iPhone 4 Apple iPhone 4s Apple iPhone 3G Apple iPhone 3GS Apple iPod Touch 4th Generation Apple iPod Classic Apple iPod Touch 3rd Generation Apple iPod Touch 2nd Generation
Please can someone help me??? Thank you :)

Replies are listed 'Best First'.
Re: Please help me Chomp !
by choroba (Cardinal) on Aug 24, 2012 at 09:42 UTC
    The only chomp I can see in your code is commented out. Does it still add a newline if you uncomment it?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Hi, unfortunately it still adds a new line if i uncomment it :(
Re: Please help me Chomp !
by nemesdani (Friar) on Aug 24, 2012 at 09:46 UTC
    It works fine for me...without the readonly...;)


    I'm too lazy to be proud of being impatient.
      Hello, How do i remove the readonlys? i've deleted them but after i've done that my output file is blank?? Thank you!!

        Did you also change the '=>' to '=' when you removed the Readonly stuff?

Re: Please help me Chomp !
by 2teez (Vicar) on Aug 24, 2012 at 11:27 UTC
    Hi,

    If I get your question right, your output is like so:

    EAN hello Apple iPhone 4 baby blue EAN hello Apple iPhone 4 baby pink EAN hello Apple iPhone 4 black ...
    and you what it like so:
    EAN hello Apple iPhone 4 baby blue EAN hello Apple iPhone 4 baby pink EAN hello Apple iPhone 4 black ...
    if so, then change this line:
    ... print OUT "\tEAN\thello $phn $colr \n"; ...
    to
    ... print OUT "\tEAN\thello $phn $colr"; ## remove the newline \n ...

    You don't have to remove your Readonly(s)!!! :)!
    Hope this helps.

Re: Please help me Chomp !
by Anonymous Monk on Aug 24, 2012 at 12:37 UTC

    It can be very helpful to use a tool like "hexdump" (by any other name) to display the content of the file byte-by-byte with accompanying text.   This will show you exactly how many newline characters are being inserted.   (If there is any garbage in the file, it will quickly show that, too.)   Perl does not insert newline characters on its own.

    Also do the same thing for the input files... what DO they, actually, byte-by-byte, contain?

      Hi,

      Perl does not insert newline characters on its own.
      Except in some cases, like when you use "say" instead of "print".

      "say Just like "print", but implicitly appends a newline. "say LIST" is simply an abbreviation for "{ local $\ = "\n"; print LIST }"."
      Please check `perldoc -f say` for more information.