Print - format / presentation question

Discussion in 'Perl Misc' started by Chris Vidal, Aug 8, 2003.

  1. Chris Vidal

    Chris Vidal Guest

    This works fine

    print "*" x 10;
    print "\n";

    But is there a way to print *'s then a newline in one line

    print "*" x 10"\n"; doesnt work.

    Tks
     
    Chris Vidal, Aug 8, 2003
    #1
    1. Advertising

  2. Chris Vidal

    Graham Gough Guest

    >>>>> "Chris" == Chris Vidal <> writes:

    Chris> print "*" x 10"\n"; doesnt work.

    print "*" x 10, "\n";
     
    Graham Gough, Aug 8, 2003
    #2
    1. Advertising

  3. Chris Vidal

    Mothra Guest

    [snipped]
    > print "*" x 10"\n"; doesnt work.
    >

    print "*" x10 . "\n";

    Mothra
     
    Mothra, Aug 8, 2003
    #3
  4. > This works fine
    >
    > print "*" x 10;
    > print "\n";
    >
    > But is there a way to print *'s then a newline in one line
    >
    > print "*" x 10"\n"; doesnt work.


    Try:

    print "*" x 10, "\n";

    HTH

    Jim
     
    James Willmore, Aug 8, 2003
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "Mothra" <> wrote in news:3f326806$:

    > [snipped]
    >> print "*" x 10"\n"; doesnt work.
    >>

    > print "*" x10 . "\n";


    Careful:

    $ perl -le 'print "*"x10."\n"'
    String found where operator expected at -e line 1, near "10."\n""
    (Missing operator before "\n"?)
    syntax error at -e line 1, near "10."\n""
    Execution of -e aborted due to compilation errors.


    - --
    Eric
    $_ = reverse sort qw p ekca lre Js reh ts
    p, $/.r, map $_.$", qw e p h tona e; print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBPzQuYWPeouIeTNHoEQIDSQCgx6lt/s8F+KNxNQvFs6yQIATr/JMAoMig
    62FX+XjzMl2FPD1fhmLGL1+Q
    =H16C
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Aug 9, 2003
    #5
  6. Chris Vidal

    Matt Garrish Guest

    "Eric J. Roode" <> wrote in message
    news:Xns93D1C369A968Csdn.comcast@206.127.4.25...
    >
    > Careful:
    >
    > $ perl -le 'print "*"x10."\n"'
    > String found where operator expected at -e line 1, near "10."\n""
    > (Missing operator before "\n"?)
    > syntax error at -e line 1, near "10."\n""
    > Execution of -e aborted due to compilation errors.


    Are you just trying to show that you can screw up the syntax, or are you
    forgetting about associativity?

    perl -le 'print("*"x10)."\n"'

    I'll assume you were trying to make the former point, but your observation
    doesn't make his response wrong, and he never claimed that it would work as
    you wrote it. Whichever the case, though, you should have explained better
    than to say "careful" and shown the error.

    Matt
     
    Matt Garrish, Aug 9, 2003
    #6
  7. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "Matt Garrish" <> wrote in
    news:eEWYa.7704$:

    >
    > "Eric J. Roode" <> wrote in message
    > news:Xns93D1C369A968Csdn.comcast@206.127.4.25...
    >>
    >> Careful:
    >>
    >> $ perl -le 'print "*"x10."\n"'
    >> String found where operator expected at -e line 1, near "10."\n""
    >> (Missing operator before "\n"?)
    >> syntax error at -e line 1, near "10."\n""
    >> Execution of -e aborted due to compilation errors.

    >
    > Are you just trying to show that you can screw up the syntax, or are
    > you forgetting about associativity?
    >
    > perl -le 'print("*"x10)."\n"'


    Are you just trying to show that you can misuse parentheses, or have you
    forgotten the "if it looks like a function call, it is a function call"
    rule?


    > I'll assume you were trying to make the former point, but your
    > observation doesn't make his response wrong, and he never claimed that
    > it would work as you wrote it.


    The way I wrote it was an exact copy, modulo whitespace, of what Mothra
    posted. I happen to have respect for Mothra, based on his or her posts
    here, which is why I didn't post a flame, and why I felt that nothing
    more than "careful" plus an error dump was needed for Mothra to
    understand that s/he should have known better than to post a quick
    solution without making sure that it was correct.

    However, I made the mistake of thinking that whitespace didn't matter to
    Mothra's solution. It does. Not because of precedence, not because of
    associativity, but because "10." is interpreted as a number, rather than
    a number plus the concatenation operator. My mistake, not Mothra's; mea
    culpa. But I didn't think that my mistake warranted a flame.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print
    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBPzRB8WPeouIeTNHoEQKduQCg+THPa8sTZ0puBtlJ0J6WsHJ8imsAoIuf
    3KWqgyDx/pe2UpejwcivLX4l
    =AGte
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Aug 9, 2003
    #7
  8. Chris Vidal

    Matt Garrish Guest

    "Eric J. Roode" <> wrote in message
    news:Xns93D1D19239CA8sdn.comcast@206.127.4.25...
    > "Matt Garrish" <> wrote in
    > news:eEWYa.7704$:
    > >
    > > perl -le 'print("*"x10)."\n"'

    >
    > Are you just trying to show that you can misuse parentheses, or have you
    > forgotten the "if it looks like a function call, it is a function call"
    > rule?
    >


    The point was to show to you why the code you posted didn't work (i.e.,
    10."\n" is misinterpreted if you leave out the whitespace). The parentheses
    are there to return the . to the operator that it was in the original
    example, and still make it work without whitespace (which I thought it was
    your intention to show wouldn't)..

    > The way I wrote it was an exact copy, modulo whitespace, of what Mothra
    > posted.


    Which means it was *not* an exact copy.

    > I happen to have respect for Mothra, based on his or her posts
    > here, which is why I didn't post a flame, and why I felt that nothing
    > more than "careful" plus an error dump was needed for Mothra to
    > understand that s/he should have known better than to post a quick
    > solution without making sure that it was correct.


    Regardless, don't you think it might have been helpful to the OP to explain
    why you were posting?

    > However, I made the mistake of thinking that whitespace didn't matter to
    > Mothra's solution. It does. Not because of precedence, not because of
    > associativity, but because "10." is interpreted as a number, rather than
    > a number plus the concatenation operator.


    Which breaks the proper associativity of the concatenation operator in the
    example posted by Mothra. If you want to look at it as going from an
    operator to not an operator that's fine with me, but it's not really here or
    there.

    > But I didn't think that my mistake warranted a flame.


    Actually, there was no intent to flame, I just couldn't figure out why you
    would post that error when it was of your own creation (I assumed you knew
    why it wasn't working, but hadn't bothered to elaborate). Reread the first
    line as a bewildered question rather than as the attack you seem to think it
    is. As I've said, it made no sense why you were posting that error other
    than to try and show how you could break his code, which isn't the most
    challenging thing to do in Perl.

    Matt
     
    Matt Garrish, Aug 9, 2003
    #8
  9. Chris Vidal

    Matt Garrish Guest

    "Sam Holden" <> wrote in message
    news:...
    >
    > Since it won't work with those parentheses that was a pretty silly way
    > of going about it.
    >


    C:\>perl -le "print('*'x10).'\n'"
    **********

    C:\>

    What doesn't work?

    Matt
     
    Matt Garrish, Aug 9, 2003
    #9
  10. Chris Vidal

    Matt Garrish Guest

    "Matt Garrish" <> wrote in message
    news:X20Za.5624$...
    >
    > What doesn't work?
    >


    My mistake. I should have tried without the -l option...

    Matt
     
    Matt Garrish, Aug 9, 2003
    #10
  11. Chris Vidal

    Matt Garrish Guest


    > "Sam Holden" <> wrote in message
    > news:...
    > >
    > > Since it won't work with those parentheses that was a pretty silly way
    > > of going about it.
    > >


    Just to clarify, I thought (mistakenly so) that I didn't need the extra
    parentheses from the following:

    print(("*"x10)."\n");

    since when I ran it from the command line it looked like it produced the
    same result.

    Matt
     
    Matt Garrish, Aug 9, 2003
    #11
  12. Chris Vidal

    Matt Garrish Guest

    "Sam Holden" <> wrote in message
    news:...
    >
    > (Keeping the '\n' strangeness you
    > introduced - I assume to get around DOS' command line parsing.)
    >


    I was just in a rush. It's actually what tipped me off to what was giving
    the extra line when I spotted that.

    Matt
     
    Matt Garrish, Aug 9, 2003
    #12
  13. Matt Garrish <> wrote:
    > "Sam Holden" <> wrote in message
    > news:...
    >>
    >> Since it won't work with those parentheses that was a pretty silly way
    >> of going about it.
    >>

    >
    > C:\>perl -le "print('*'x10).'\n'"
    > **********
    >
    > C:\>
    >
    > What doesn't work?



    perl -le "print('*'x10)"

    works exactly the same.

    That's a clue, the '\n' part is in void context, it doesn't get output.

    Enabling warnings often provides clues.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 9, 2003
    #13
  14. Chris Vidal

    Matt Garrish Guest

    "Tad McClellan" <> wrote in message
    news:...
    >
    > Enabling warnings often provides clues.
    >


    Which I normally do (see previous post for the working example I started
    with). I didn't want to be guilty of tinkering with the example provided by
    Eric (which was his downfall earlier), and since he didn't have warnings
    enabled, and since I already knew the original was parsable... I should have
    known I was making too many assumptions.

    In writing it for the command line, I decided to tinker a little and see if
    perl would still handle the expression without the outer braces, which it
    did, albeit only in appearance, so I posted the one-liner without any
    further thought. Not specifying the useless -l option would have been as
    effective, as the lack of a newline would have been just as informative as
    anything the warnings had to offer, but you are right.

    Matt
     
    Matt Garrish, Aug 9, 2003
    #14
  15. Chris Vidal

    Mothra Guest

    Hi Eric,

    [snipped]

    > The way I wrote it was an exact copy, modulo whitespace, of what Mothra
    > posted. I happen to have respect for Mothra, based on his or her posts
    > here, which is why I didn't post a flame, and why I felt that nothing
    > more than "careful" plus an error dump was needed for Mothra to
    > understand that s/he should have known better than to post a quick
    > solution without making sure that it was correct.
    >

    I do my best to test before I post. (I like all who have been here for some
    time
    have made the mistake of posting bad code and have paid the price
    for it) In this case I did test. Here is what I did.

    use strict;
    use warnings;
    use diagnostics;

    print "*" x10 . "\n";

    I did not post the first 3 lines (stupid me) If I had it would
    have been clearer for all.

    > However, I made the mistake of thinking that whitespace didn't matter to
    > Mothra's solution. It does.


    The whitespace does make a difference! (I did the same thing ie:
    left out the white space and received the same error that you did)
    That is why I added the whitespaces.

    > Not because of precedence, not because of
    > associativity, but because "10." is interpreted as a number, rather than
    > a number plus the concatenation operator. My mistake, not Mothra's; mea


    No problem :)

    Mothra
     
    Mothra, Aug 11, 2003
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rozwel
    Replies:
    2
    Views:
    400
    roz wel
    Feb 13, 2005
  2. Replies:
    0
    Views:
    581
  3. Replies:
    5
    Views:
    729
    Magnus Lycka
    Oct 27, 2006
  4. keto
    Replies:
    0
    Views:
    1,001
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    374
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page