Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Problem printing @arrays

by sbrothy (Acolyte)
on Sep 04, 2021 at 02:55 UTC ( [id://11136431]=perlquestion: print w/replies, xml ) Need Help??

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

PROGRAM: FOO.PL:
#!/usr/bin/perl use autodie; use diagnostics; use strict; use warnings; use 5.010; my @array = ('a', 'b', 'v', 'h', 'j'); print "@array\n";
OUTPUT:

a b v h j

PROGRAM: BAR.PL:
#!/usr/bin/perl use autodie; use diagnostics; use strict; use warnings; use 5.010; ########################################### # read word list (csw.txt contains all the # valid words from the CSW scrabble word list) open my $fh, '<', 'csw.txt' or die $!; chomp (my @words = <$fh>); close $fh; print "@words\n";

OUTPUT:
If I execute it from the CLI like ./BAR.PL output is: ZZZSYVASSLYSLUS
it like all the words are printed the same place without any newlines.

If I execute it from the CLI like

./BAR.PL &> output

output contains: AA AAH AAHED AAHING AAHS AAL DELETIA ZYTHUMS ZYZZYVA ZYZZYVAS ZZZ ZZZS

I'm at a loss to explain what it going on.

PS: BTW. I'm ofcourse not trying to cheat at scrabble. I just like challenge. :) Regards, sbrothy

Replies are listed 'Best First'.
Re: Problem printing @arrays
by tybalt89 (Monsignor) on Sep 04, 2021 at 07:08 UTC

    Try replacing

    chomp (my @words = <$fh>);
    with
    tr/\r\n//d for my @words = <$fh>;
    which will also remove the \r if present.

      Thank you Tybalt89. That did the trick. I'ts kinda funny though, I had a working script based on one of your older posts here.

      At that time it worked fine. Possibly I downloaded the file from somewhere else this time.

      I have other issues now but I will give 'em a go before asking for help again.

      Thank you!

Re: Problem printing @arrays
by AnomalousMonk (Archbishop) on Sep 04, 2021 at 03:58 UTC

    I suspect you are reading a file created with \r\n (carriagereturn-linefeed) line endings (e.g., on a Windows system) on a system (e.g., *nix) that uses \n (linefeed) line endings. On the reading system, the line-feed is chomp-ed off leaving the carriage-return. This can be simulated with something like:

    Win8 Strawberry 5.8.9.5 (32) Fri 09/03/2021 23:52:22 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dump qw(dd); my $data = <<"EOD"; AA\r AAH\r AAHED\r AAHING\r AAHS\r AAL\r ZYTHUMS\r ZYZZYVA\r ZYZZYVAS\r ZZZ\r ZZZS\r EOD open my $fh, '<', \$data or die $!; my @words = <$fh>; close $fh; dd \@words; chomp @words; dd \@words; print "@words\n"; ^Z [ "AA\r\n", "AAH\r\n", "AAHED\r\n", "AAHING\r\n", "AAHS\r\n", "AAL\r\n", "ZYTHUMS\r\n", "ZYZZYVA\r\n", "ZYZZYVAS\r\n", "ZZZ\r\n", "ZZZS\r\n", ] [ "AA\r", "AAH\r", "AAHED\r", "AAHING\r", "AAHS\r", "AAL\r", "ZYTHUMS\r", "ZYZZYVA\r", "ZYZZYVAS\r", "ZZZ\r", "ZZZS\r", ] ZZZSYVAS


    Give a man a fish:  <%-{-{-{-<

      you forgot to give a solution... :crlf

      I'll have to study this a little more. The dd command isn't what I would call intuitive. I'm sure, though, that I'll learn something by it I cannot learn any other way; much like holding a cat by the tail :)

      Regards, sbrothy.

Re: Problem printing @arrays
by BillKSmith (Monsignor) on Sep 04, 2021 at 23:13 UTC
    Now that you know that you have a windows file, it seems to me that the best solution is to read it the way perl on windows would. (Note: The IO-layer :crlf is the default on windows.)
    open my $fh, '<:crlf', 'csw.txt' or die $!; chomp (my @words = <$fh>); close $fh;
    Bill
Re: Problem printing @arrays
by karlgoethebier (Abbot) on Sep 04, 2021 at 08:29 UTC

    BTW, if you autodie you don’t need to die. As far as I remember. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      You're ofcourse absolutely right. These problems must have made me a litle paranoid! :)

      Thank you and regards,sbrothy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11136431]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found