Re: Printing a hollow square of Asterisks
by Fletch (Bishop) on Mar 04, 2009 at 14:09 UTC
|
I'd say try printing spaces rather than just asterisks.
/me starts humming Nirvana's lesser known B side "Smells Like Homework" . . .
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] |
|
|
| [reply] |
Re: Printing a hollow square of Asterisks
by JavaFan (Canon) on Mar 04, 2009 at 16:05 UTC
|
print <<' **';
****
* *
* *
****
**
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: Printing a hollow square of Asterisks
by Anonymous Monk on Mar 04, 2009 at 14:10 UTC
|
my $x = 4;
my $y = 4;
for my $xx (1..$x) {
for my $yy(1..$y) {
if( something ){
print ' ';
} else {
print "*";
}
}
print "\n";
}
| [reply] [d/l] |
|
|
this code doesnt output anything??
| [reply] |
|
|
Maybe you thought the something in
if( something ){
was an actual instruction or variable?
If so, use strict; use warnings; would have disabused you of that notion.
| [reply] [d/l] [select] |
|
|
$ perl test.pl |od -ta
0000000 sp sp sp sp nl sp sp sp sp nl sp sp sp sp nl s
+p
0000020 sp sp sp nl
0000024
$ perl test.pl |od -tx1
0000000 20 20 20 20 0a 20 20 20 20 0a 20 20 20 20 0a 20
0000020 20 20 20 0a
0000024
$
You're supposed to replace something with a simple expression that evaluates to true when you want to print spaces. | [reply] [d/l] |
Re: Printing a hollow square of Asterisks
by hbm (Hermit) on Mar 04, 2009 at 17:04 UTC
|
Tough to beat JavaFan's, but here's a fairly plain-English method:
use strict;
use warnings;
my $x = 4; # width
my $y = 2; # number of "hollow" rows
my $solid = '*' x $x . $/;
my $hollow = '*' . ' ' x ($x-2) . '*' . $/;
print $solid,
($hollow) x $y,
$solid;
| [reply] [d/l] |
|
|
thank you so much... it works
| [reply] |
|
|
Now tell us how you think it works and explain each interesting line. Marks will be awarded according to the quality of your answer.
Also identify any situations where it will fail and suggest appropriate handling for those situations.
True laziness is hard work
| [reply] |
Re: Printing a hollow square of Asterisks
by puudeli (Pilgrim) on Mar 04, 2009 at 14:10 UTC
|
Sounds like homework to me :-P. Anyway, you need to figure out an algorithm that removes the asterisks in the middle on rows 2 & 3.
--
seek $her, $from, $everywhere if exists $true{love};
| [reply] [d/l] |
|
|
homework? nah, im 24 and self studying for Perl.. Come across this question but cant figure it out.
| [reply] |
|
|
Ah, so it is homework after all :-)!
Even self-imposed homework is homework, but in this case I think you will be a willing participant if I tell you we'll be happy to help you think it through and even tell you once you've got it right, but not to give the answer. :-)
So here is a review of your code:
# *always* use these - they do (some of) your error
# checking for you! - saves *lots* of time
use strict;
use warnings;
#this is right
my $x = 4;
my $y = 4;
for (1..$x) {
#this bit is wrong - see discussion below
print "*" x $y, "\n";
}
The bit I marked wrong is where your problem is. To get
a hole in the middle you need dark space for the edges, i.e. asterisks and white space for the hole, i.e. spaces. If you print "*" x $y you will just get the dark stuff and no white space. So how do you get white space? Ask yourself these questions:
- How many rows do you want to mark the top edge of the box? You can use a solid line of asterisks for each row in the edge since an edge is solid with no whitespace.
- How many rows do you want to mark the bottom edge of the box?
- How many rows are left for the part between the top and bottom edge? If you want to see a hole, each of the lines in the middle will need some white space in it.
- Now inside your for loop, can you think of a way to tell if you are on the top edge, middle, or bottom edge?
- Can you use that to create an if...elsif...else statement that does different things depending on whether you are on the top, middle or bottom?
Printing out the top and bottom is easy: you have that bit already - just print out a solid line of asterisks. But what about the middle? Here's how to think that through:
- How many asterisks do you want for your sides: 1? 2?
- If each of your edges has N asterisks and your total line size is M, how many spaces will you need in the middle?
- Can you think of a way to print out three separate strings on a single line to make a line with a hole in it: one containing N asterisks (left edge), one containing the spaces (middle), and one containing N asterisks (right edge)?
When you've thought this through, update your original post with the changes. If it still isn't working quite right, I'm sure people will be glad to help you further.
Best, beth | [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
perl -le 'my $n = shift; die q{Please provide a positive number.} if ($n < 0); print q{*} x $n; if ($n > 1) { foreach (2 .. $n - 1) { print q{*}, q{ } x ($n - 2), q{*}; } print q{*{ x $n; }'
The above snippet takes a number afterward to select the size. In the case of nothing given, $n gets a value of 0. The q{*} x $n will give nothing if $n is 0, or $n asterisks. The if statement prevents the loop for the sides and printing a bottom if $n is 1. The loop is triggered only if $n - 1 is greater than 2 (ie, 3 or more), and prints only the asteristks on either edge, producing the "hole" in the middle.
Hope that helps.
| [reply] [d/l] |
|
|
|
|
|
|
Re: Printing a hollow square of Asterisks
by planetscape (Chancellor) on Mar 05, 2009 at 03:00 UTC
|
Not really an answer to your question, but some may find this interesting/fun: boxes.
| [reply] |