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

How to print the following pattern:

* ** *** ** *

Replies are listed 'Best First'.
Re: asterisk pattern
by GrandFather (Sage) on Oct 27, 2011 at 10:54 UTC
    print <<PATTERN; * ** *** ** * PATTERN
    True laziness is hard work
      But he wants it in a loop! So, I propose:
      do {1;} until print <<"*"; * ** *** ** * *
Re: asterisk pattern
by moritz (Cardinal) on Oct 27, 2011 at 11:48 UTC

    I'd go with this solution:

    use 5.014; use warnings; use Mojo::UserAgent; my $text = Mojo::UserAgent ->new ->get('http://www.perlmonks.org/?node_id=934101') ->res ->dom ->at('tt') ->all_text; chomp $text; print $text;
Re: asterisk pattern
by davido (Cardinal) on Oct 27, 2011 at 11:12 UTC

    Would your teacher accept this?

    perl -E 'say for ("  *", " **", "***" )[0,1,2,1,0]'

    (Probably not, but it might be worth the 'F' just to see the look on your teacher's face.)


    Dave

      but it might be worth the 'F' just to see the look on your teacher's face

      Obligatory link: Niels Bohr and the barometer (not true, unfortunately.)

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: asterisk pattern
by Corion (Pope) on Oct 27, 2011 at 11:35 UTC

    The following works for me:

    $b=this_code_is_from_perlmonks_org; for my $a(some_program_was_written_for_perlmonks_org=~/[st]/g){ print $a,sprintf '%4s','*'x(2+($a cmp $b)), qq(\n); $b=$a }

    Update: I didn't get the requirement of outputting different depths, so here is an improved variation on the same theme:

    $level=12; my $i=12; my $l=$level-12; $_ = #$; this_program_source_was_written_for_the_site_perlmonks_org. please_do_not_give_this_code_to_your_teacher_without_understanding. We_value_learning_but_the_work_has_to_be_done_by_yourself. kthxbai; $b=a_program_written_by_Corion; for $a(($_.reverse)=~/[sx]/g){ $d=($a cmp $b)||$d; $l+=$d; $b=$a; print sprintf '%'.$i.'s', '*'x$l, qq(\n) unless $l<0 }
Re: asterisk pattern
by davido (Cardinal) on Oct 27, 2011 at 10:54 UTC

    perl -e 'print qq/  *\n **\n***\n **\n  *\n/;'

    Dave

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: asterisk pattern
by Ratazong (Monsignor) on Oct 27, 2011 at 12:45 UTC
    So many possibilities! The next one, however requires some human interaction:
    use strict; use warnings; print "Ssssshhh! Please turn your monitor first (otherwise the pyramid + might not point to the correct direction).\n"; my $Ss = '*'; my $ss = 2; my $sS = $Ss x $ss; for (0..$ss) { print "\n". $sS. $Ss. reverse $sS; $sS =~ s/\S/ /; }
Re: asterisk pattern
by choroba (Archbishop) on Oct 27, 2011 at 11:06 UTC
    say' 'x(3-$_),'*'x$_ for 1..3,2,1
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: asterisk pattern
by BrowserUk (Pope) on Oct 27, 2011 at 11:23 UTC

    sub la{ my $n = shift; print ' ' x ( $n - $_ ), '*' x $_ for 1 .. $n, reverse 0 .. $n - 1; };; la(3);; * ** *** ** * la(6);; * ** *** **** ***** ****** ***** **** *** ** *

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: asterisk pattern
by GrandFather (Sage) on Oct 27, 2011 at 11:03 UTC

    Oh! I see from the CB that you want to use a for loop:

    print ' ' x (abs(2 - $_)), '*' x (3 - abs(2 - $_)), "\n" for 0 .. 4;
    True laziness is hard work
Re: asterisk pattern
by choroba (Archbishop) on Oct 27, 2011 at 11:15 UTC
    No explicit loop, just recursion and repetition:
    $p=1;sub p{my$x=shift;$x||exit;say" "x(3-$x),"*"x$x;$p=-1if$x>2;p($x+$ +p)}p 1
    Update: Not so funny in C:
    #include<stdio.h> int p,n;void q(int x){int j;if(!x)return;for(j=1;j<=n-x;j++)printf(" " +);for(j=1;j<=x;j++)printf("*");printf("\n");if(x>=n)p=-1;q(x+p);}void + main(){printf("Enter the number of columns: ");scanf("%d",&n);p=1;q( +1);}
    (I've never really used C, anyway.)