Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Print format not working

by catfish1116 (Beadle)
on Oct 04, 2018 at 17:01 UTC ( #1223544=perlquestion: print w/replies, xml ) Need Help??

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

below is my code:

#!/bin/perl use v5.10; say "Please enter a few lines of text. Press crtl-D when finished"; @text_lines = <STDIN>; chomp($text_lines); say "Please enter the width of the column: "; $column = <STDIN>; $ruler = "1234567890"; say "$ruler" x $column; foreach (@text_lines) { #printf "%${column}s\n", $_; printf "%${column}s\n", $_; }

Here is my output:

Please enter a few lines of text. Press crtl-D when finished

rrrrr eeeeee tyyyyy

Please enter the width of the column: 10 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

%10

s

%10

s

%10

s

I copied the code straight out of Learning Perl (7th edition). I typed in 3 lines of text as well as the width I wanted. The output should have been the repeating numbers, (1-0), and then below the text lines with aligned columns, (width called in by <STDIN>. I guess it worked ok for the authors ... not so much for me though .... Any help would be appreciated.

Thanks, Catfish

Replies are listed 'Best First'.
Re: Print format not working
by toolic (Bishop) on Oct 04, 2018 at 17:26 UTC

      Declaring the text_lines as my @text_lines & chomping column worked !!!

      Thank you !! Catfish
Re: Print format not working (updated)
by AnomalousMonk (Bishop) on Oct 04, 2018 at 17:35 UTC
    @text_lines = <STDIN>; chomp($text_lines);

    The statement  chomp($text_lines); chomp-s the (nonexistent) scalar  $text_lines not the  @text_lines array. Always

    use warnings; use strict;
    at the start of your scripts. (Update: Actually, it's not accurate to describe  $text_lines as "nonexistent": if strictures are not enabled (see strict), just referring to such a variable causes Perl to call it into existence. This is known as autovivification (see perlglossary), and such a scalar is initialized with a default value of undef. However, chomp-ing undef doesn't do very much, and the script never accesses  $text_lines anywhere else, so...)

    $column = <STDIN>;

    This leaves  $column un-chomp-ed.

    say "$ruler" x $column;

    Not sure what this is supposed to do.

    rrrrr eeeeee tyyyyy

    This is just one line.

    c:\@Work\Perl\monks>perl -le "use 5.010; ;; use warnings; use strict; ;; say 'Please enter a few lines of text. Press ctrl-Z when finished'; my @text_lines = <STDIN>; chomp(@text_lines); ;; say 'Please enter the width of the column: '; my $column = <STDIN>; chomp $column; ;; my $ruler = '1234567890'; say $ruler x $column; ;; foreach (@text_lines) { printf qq{%${column}s\n}, $_; } " Please enter a few lines of text. Press ctrl-Z when finished xyzzy now is the time for all good men x foo bar ^Z Please enter the width of the column: 7 1234567890123456789012345678901234567890123456789012345678901234567890 xyzzy now is the time for all good men x foo bar
    (Run under Windoze, so had to use ^Z instead of ^D.)

    Update: Try
        printf qq{%*s\n}, $column, $_;
    as an experiment (see * format specifier in sprintf specifier definitions).
    (Update: Tried that? Ok, now try
        printf qq{%*.*s\n}, $column, $column, $_;
    also.)


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-05-22 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (80 votes). Check out past polls.

    Notices?