Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

outputting fun

by ANKUR (Novice)
on Aug 29, 2000 at 19:30 UTC ( #30151=perlquestion: print w/ replies, xml ) Need Help??
ANKUR has asked for the wisdom of the Perl Monks concerning the following question:

HI
This is just another perl newbie here.Thanks every1 for replying to my question about merging files.
.Phew! i really do have a long way to go. Anyway i was going through some C exercises i had done previously and i came across an interesting
exercise. You have to write a program to produce
the following output
Well, um i wrote the code , but since er i just baby talk perl,
the code is very big i just know that there
is a more better and more intelligent way to do it
.I will really appreciate your help perl monks if you kindly
SHOW A BETTER WAY TO DO IT
THANKS ankur
ABCDEFGFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A <CODE> #!/usr/bin/perl -w @store=('a'..'g'); $i=@store; $count=0; while($i>=0){ &printline($i-1,@store); &revline($i-1,$count,@store); $count++; pop(@store); } sub printline{ ($itemp,@printarray)=@_; for($j=0;$j<=$itemp;$j++){ print "$printarray[$j]"; } } sub revline{ ($it,$cou,@linarray)=@_; if ($cou==0){ for($k=$it;$k>=0;$k--){ print "$linarray[$k]"; } print '/n'; return; } $p=$cou-1; $r=2*$p + 1; for ($j=1;$j<=$r;$j++){ print " "; } for($k=@linarray-1;$k>=0;$k--){ print "$store[$k]"; if ($k==0){ print '\n' ; } } }
er i dont know formatting yet

Comment on outputting fun
Download Code
Re: outputting fun
by Corion (Pope) on Aug 29, 2000 at 19:47 UTC

    Here's my try at it. It uses two forms of string replacement - one transliteration, replacing every G with a space, and one regular expression, replacing a single character, followed by one or more spaces and then that character again, with two more spaces.

    #!/usr/bin/perl -w use strict; my $line; print( $line = join( "", ('A' .. 'G'), reverse ( 'A'..'F' )), "\n" ); $line =~ tr/G/ /; print( $line, "\n" ); while ($line !~ /^\s+$/) { $line =~ s/(\S)(\s+)\1/ $2 /; print( $line, "\n"); };
    This is obviously not the smallest possible code, but a bit more Perlish and elegant (IMholyO) :-)

      Just would like to say that Iam a wingnut :)
      for(my($c)=71,my($i)=0; $c >= 65; $c--,$i++) { print ( (chr(65)..chr($c)), (' 'x($i+($i-1))), reverse(chr(65)..chr(($c==71)? $c-1 : $c)), "\n" ); }
      p.s. I broke it into multi-line so that it was readable :)



      lindex
      /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/
      There is the obvious
      print <<EOF; ABCDEFGFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A EOF
      How about?
      $a = "ABCDEFGFEDCBA\n"; print($a), substr($a, 7-$_, 2*$_-1) = " " ." " x ($_-1) for 1..7
      -- stef
Re: outputting fun
by Boogman (Scribe) on Aug 29, 2000 at 19:51 UTC
    Here's another way. I just printed the first line cause it didn't really fit the pattern used for the rest of the lines...
    print 'ABCDEFGFEDCBA', "\n"; my @letters = ( 'A' .. 'F' ); for ( 0 .. $#letters ) { print @letters[ 0 .. $#letters - $_ ], ' ' x ( 1 + 2 * $_ ), reverse( @letters[ 0 .. $#letters - $_ ] ), "\n"; }
    A bit shorter, but I must say I like Corion's solution better.
RE: outputting fun
by Shendal (Hermit) on Aug 29, 2000 at 19:51 UTC
    Here's my entry:
    #!/usr/bin/perl -w use strict; # always $_ = join("",('A' .. 'G'), reverse('A' .. 'F'),"\n"); foreach my $letter (reverse ('A' .. 'G')) { print; s/$letter/ /g; }

    Cheers,
    Shendal
Re: outputting fun
by indigo (Scribe) on Aug 29, 2000 at 20:08 UTC
    #!/usr/bin/perl -w use strict; my $string = 'ABCDEFGFEDCBA'; print "$string\n" and $string =~ s/$_/ /g for reverse 'A' .. 'G';
Re: outputting fun
by tye (Cardinal) on Aug 29, 2000 at 20:43 UTC
    #!/usr/bin/perl -l print reverse join"\n",reverse map{reverse sprintf "%-6.6s%7s",(reverse join"",@$_=reverse@$_),reverse join "",reverse@$_}reverse map{[reverse'A'..$_]}reverse'A'..'G'

    I think I can eliminate one of those reverses, but I'm not sure which one. (:

            - tye (but my friends call me "Tye")
      how about eliminating 8 of them:
      #!/usr/bin/perl -l print join $/, map { sprintf "%-6.6s%7s", join("", @$_), join("", reverse @$_) } map { [ 'A' .. $_ ] } reverse 'A'..'G';
Re: outputting fun
by cwest (Friar) on Aug 29, 2000 at 22:51 UTC
    BEGIN{$_=7 # Change this number to control pivot -1}foreach $.(int(length()/2)..length()-2){print ((/\s/?s/\w(\s+)\w/ $1 /:s/(\w{$;})./$1 /)?$_:1) ;}BEGIN{($;,$_,$\)=($_,join(''=>chr(65)..chr(65+ $_),reverse(chr(65)..chr(65+$_-1))),$/);print}
    because I'm nuts, that's why :-)
    --
    Casey
    
Re (tilly) 1: outputting fun
by tilly (Archbishop) on Mar 28, 2001 at 05:17 UTC
    Another silly entry.
    $_="ABCDEFGFEDCBA\n"; print; print while s/(?<!^)(G|\b\w|\w\b)(?!$)/ /g;
Re: outputting fun
by Arry (Initiate) on Jan 09, 2002 at 19:16 UTC
    OK, I'm new to this, but thought I would have a go. On my own I get: $d="ABCDEFGFEDCBA"; for($i=71;$i>64;$i-=1) { print "$d\n"; $c=chr($i); $d =~ s/$c/ /g; } With a little help, I then got: $_="ABCDEFGFEDCBA\n";for $r(reverse(A..G)){print; s/$r/ /g} Same theory, better solution.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-11-01 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (229 votes), past polls