Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

repeat characters in text

by Anonymous Monk
on May 02, 2007 at 17:37 UTC ( #613222=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i can't figure this out, and I know this must exist in perl either as a construct, or a print/sprintf method...


I'm looking for an idiomatic way to essentially print an "a" followed by 5 "!" characters

ie: print ( "a" . ( "!" * 5 ) )

does that make sense? how the hell do i do this ?

Comment on repeat characters in text
Re: repeat characters in text
by cog (Parson) on May 02, 2007 at 17:44 UTC
    print ( "a" . ( "!" x 5 ) )

    That'll do the trick O:-)

Re: repeat characters in text
by Albannach (Prior) on May 02, 2007 at 17:46 UTC
    You have the right idea, you just need a different operator - print 'a', '!' x 5; will do it. Though Perl doesn't care, I prefer not to use double quotes unless I expect interpolation to be taking place.

    --
    I'd like to be able to assign to an luser

      You have the right idea, you just need a different operator - print 'a', '!' x 5; will do it. Though Perl doesn't care, I prefer not to use double quotes unless I expect interpolation to be taking place.
      print "a@{['!' x 5]}"; # :-)
Re: repeat characters in text
by artist (Parson) on May 02, 2007 at 19:00 UTC
    Without parenthesis and double quotes.
    print 'a'.'!'x5;
    --Artist
Re: repeat characters in text
by johngg (Abbot) on May 02, 2007 at 19:49 UTC
    Slightly OT but I noticed that two solutions print a string concatenation while another supplied a list of strings to print. I wondered if there was any performance difference between the two. It seems that using a list is consistently about 10% faster. Here's the benchmark code.

    use strict; use warnings; use Benchmark q{cmpthese}; my $nullFile = q{/dev/null}; open my $nullFH, q{>}, $nullFile or die qq{open: $nullFile: $!\n}; my $rcConcat = sub { print $nullFH q{a} . q{!} x 5 for 1 .. 10000; }; my $rcList = sub { print $nullFH q{a}, q{!} x 5 for 1 .. 10000; }; cmpthese (-3, { Concat => $rcConcat, List => $rcList, }); close $nullFH or die qq{close: $nullFile: $!\n};

    and the output from 10 runs.

    Cheers,

    JohnGG

Re: repeat characters in text
by GrandFather (Cardinal) on May 02, 2007 at 20:53 UTC

    "Multiple exclamation marks are a sure sign of a diseased mind." (Terry Pratchet: Eric). ;)


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-08-28 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls