Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2003
    Location
    san jose, CA
    Posts
    68

    Unanswered: creating a figure pattern using perl

    Dear perl forum member:

    I am learing perl language. I am trying to do a exercise which request do generate a pattern like this. the stars from column 2-10 are missing, only first and last stars are present.

    ***********
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    ***********
    i create two varibles height = 9; width= 11;

    I used nested for loops(two of them) try to create this pattern, but I couldn't figure how to manipulate the height and width, so the reult will look like the answer.

    here is my for loops, please give me some suggestions or hints so I can learn from it. Thanks!

    for($i = 0; $i < $height; $i++)
    {
    #inner for loop will generate the columns
    for($j = 0; $j < $width; $j++)
    {
    if($j == 0 || $ j == 10)--# first and last row missing full pattern
    if($i == 0 || $i == 8)--- # first and last start in row 2-8 are missing

    {
    print "*";
    }
    else
    {
    print " ";
    }
    }
    #after previous row created, move cursor to the next row
    print "\n";

    }
    Last edited by sjgrad03; 10-11-04 at 18:44.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I suppose that this is really evil, because although it works it will make you learn more about Perl than you really need for this problem if you want to explain it to your teacher, and it is decidedly NOT perl-think, but:
    Code:
    my ($h, $w) = (9, 11);
    my ($x, $y);
    
    for ($y = 1; $y <= $h; $y++) {
       for ($x = 1; $x <= $w; $x++) {
          print $y == 1 || $x == 1 || $y == $h || $x == $w ? "*" : " ";
          };
       print "\n";
       };
    Keep in mind that you should only play with this if you want to learn about Perl... If you are looking for an easy way to get your assignment done, this is probably a really bad choice!

    -PatP

  3. #3
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    how about:

    Code:
    ($w, $h) = (11, 9); $, = "\n";
    print '*' x $w, ('*'.(' ' x ($w - 2)).'*') x ($h - 2), '*' x $w, '';
    $, = "\n" sets the output field separator to newline. That means that when print is passed a list of arguments, it will separate each with a newline. Handy, but dangerous because $\ is a global variable. See perldoc perlvar for more info.

    Let's take print's arguments apart:

    '*' x $w : see perldoc perlop for a description of the 'x' operator. Here it takes a string and repeats it $w times. This returns a string.

    (' ' x ($w - 2)) : Similar to the above, but note two things: $w - 2 is in parentheses due to precedence rules. So this is $w - 2 spaces.

    '*'.(' ' x ($w - 2)).'*' : Use the concatenation operator to put stars before and after the spaces.

    ('*'.(' ' x ($w - 2)).'*') x ($h - 2) : Because of the parens, ('*'.(' ' x ($w - 2)).'*') is actually a single-item list. We then repeat that list $h - 2 times, making the inside of our figure.

    , '' : That's just to force another newline for neatness.

    Thus each line to be printed is an item in the list, so print's output field separator puts newlines after each one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •