Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Print a string X times?

by rastoboy (Monk)
on Aug 03, 2010 at 23:47 UTC ( [id://852770]=perlquestion: print w/replies, xml ) Need Help??

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

I know this is a silly question, but it's really bugging me. I need to print a bunch of -'s of an arbitrary computed length, such as:

-------

or maybe:

-----------------

At the moment I compute the length needed and do:

while ($length) { $separator .= '-'; }
I could have sworn I've read a simple way to simply say "print X this many times". It's just not very elegent or "perlish" seeming and I'd appreciate any feedback you care to give!

Replies are listed 'Best First'.
Re: Print a string X times?
by almut (Canon) on Aug 03, 2010 at 23:53 UTC
Re: Print a string X times?
by toolic (Bishop) on Aug 04, 2010 at 00:02 UTC
Re: Print a string X times?
by BrowserUk (Patriarch) on Aug 03, 2010 at 23:53 UTC
      OMG lol. I knew I'd seen something simple like that.

      Thank you very much.

Re: Print a string X times?
by cdarke (Prior) on Aug 04, 2010 at 06:01 UTC
    This is my personal favorite:
    local $, = ' '; print +('-') x $length,"\n";
      in the statement print +('-') x $length , "\n";

      the +('-') does some magic which I don't understand, could you please explain that statement..


      Vivek
      -- 'I' am not the body, 'I' am the 'soul/consciousness', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.

        From perlop: Symbolic Unary Operators:

        "Unary "+" has no effect whatsoever, even on strings. It is useful syntactically for separating a function name from a parenthesized expression that would otherwise be interpreted as the complete list of function arguments."

        Try removing the + and ensure you use strictures (well, warnings anyway) and see what happens.

        True laziness is hard work

      That's really usefull!

      ++ for that!


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Print a string X times?
by ikegami (Patriarch) on Aug 04, 2010 at 16:03 UTC
    And of course, there's
    print '-' for 1..$length;

    Previously mentioned "x" should be a bit faster at the cost of more memory.

      Or let's go for some obfuscation:

      perl -e 's;^$;sprintf"%80s",$/;e;y; ;-; ;print'

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Log In?
Username:
Password:

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

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

    No recent polls found