Integer Repetition | Familiar Sight | | Join Date: Nov 2006
Posts: 168
| | |
Hi All,
i need help with the following:
i need to write a simple program that asks the user to input a number (minimum of 3 digits), which i seperate into individual digits and print the 1st digit 1 time, 2nd digit 2 times, 3rd digit 3 times and so forth...
I cannot get the last bit working, what bit of code can i use for the repetition part?
many thanks
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: Integer Repetition
Post the code you have wrttien so far to try and solve this problem. Even if it does not work. We can then see where you are making a mistake and help you better. Don't expect anyone to write code for you based upon your programming requirements, we will want to see effort on your part first to solve the problem.
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: Integer Repetition
hehehe.... seems that the thought of having to write this very simple code his/her self was a bit too much to contemplate.
| | Familiar Sight | | Join Date: Nov 2006
Posts: 168
| | | re: Integer Repetition
KevinADC,
sorry mate for the late reply, well busy.
ok i think ive cracked it with 5digits: - print "please insert a 5-digit integer: \n";
-
-
$string = <STDIN>;
-
-
$var = substr($string,0,1) x 1;
-
$var2 = substr($string,1,1) x 2;
-
$var3 = substr($string,2,1) x 3;
-
$var4 = substr($string,3,1) x 4;
-
$var5 = substr($string,4,1) x 5;
-
-
print $var."\n", $var2."\n", $var3."\n", $var4."\n", $var5."\n";
Im new to Perl, so im sure this can be written in a smaller way..
Can you please check this for me?
thanks
|  | Expert | | Join Date: Dec 2007
Posts: 400
| | | re: Integer Repetition
To reduce the number of lines, you could have used for loop: -
print "please insert a 5-digit integer: \n";
-
-
$string = <STDIN>;
-
for($i=1;$i<=5;$i++) {
-
my $var = substr($string,$i-1,1) x $i;
-
print "$var\n";
-
}
-
Another way of doing it would be : -
print "please insert a 5-digit integer: \n";
-
$string = <STDIN>;
-
$i=1;
-
print $1x$i++."\n" while($string=~/(\d)/g); #works for any no. of digits
-
|  | Member | | Join Date: Jul 2008 Location: San Antonio, Texas
Posts: 68
| | | re: Integer Repetition
Just a suggestion - If this is an assignment for a programming class, and the instructor is an old fortran programmer, he/she will probably want your perl code to look exactly like the fortran code they are familar with and a one-liner will get an "F".
| | Familiar Sight | | Join Date: Nov 2006
Posts: 168
| | | re: Integer Repetition Quote:
Originally Posted by nithinpes To reduce the number of lines, you could have used for loop: -
print "please insert a 5-digit integer: \n";
-
-
$string = <STDIN>;
-
for($i=1;$i<=5;$i++) {
-
my $var = substr($string,$i-1,1) x $i;
-
print "$var\n";
-
}
-
Another way of doing it would be : -
print "please insert a 5-digit integer: \n";
-
$string = <STDIN>;
-
$i=1;
-
print $1x$i++."\n" while($string=~/(\d)/g); #works for any no. of digits
-
Can you please provide me some explaination for the above code?
thanks
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: Integer Repetition Quote:
Originally Posted by Beany KevinADC,
sorry mate for the late reply, well busy.
ok i think ive cracked it with 5digits: - print "please insert a 5-digit integer: \n";
-
-
$string = <STDIN>;
-
-
$var = substr($string,0,1) x 1;
-
$var2 = substr($string,1,1) x 2;
-
$var3 = substr($string,2,1) x 3;
-
$var4 = substr($string,3,1) x 4;
-
$var5 = substr($string,4,1) x 5;
-
-
print $var."\n", $var2."\n", $var3."\n", $var4."\n", $var5."\n";
Im new to Perl, so im sure this can be written in a smaller way..
Can you please check this for me?
thanks The above code is probably fine for your assingment. When you ask on a forum you will get solutions that are probably beyond your current lesson in class. You should stick with your own code as long as it works properly.
| | Familiar Sight | | Join Date: Nov 2006
Posts: 168
| | | re: Integer Repetition Quote:
Originally Posted by KevinADC The above code is probably fine for your assingment. When you ask on a forum you will get solutions that are probably beyond your current lesson in class. You should stick with your own code as long as it works properly. thanks for the advice,
I'm self studying to learn Perl so have decided to do a chapter a week.
Also, can you please give me a hint or any tips as to where to look (or what function to use) if i wanted to create a similar code but instead the inputted integer prints a square of asterisks..
for eg, if a user inputs an integer of '7' the output would show a square of asterisks (7X7)..
regards
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | re: Integer Repetition
The output would not be perfectly square, or square at all because the line height of a character is greater than it's width, or it is with html, at least. :P
Anyway, the logic goes as follows: - Get your limits - you mentioned before a square of 7x7. So, of course, for a square you will only need 1 limit, but for the sake of portability, we'll take a limit for the X column and the Y column. So, limitX = 7 and limitY = 7.
- Now we'll need to use an outer-loop for the columns and a loop within that for the actual output. The outer for loop should loop from 1 - LimitY.
- Inside this outer-loop we need another for loop to actually output our character of choice - in this case, an asterix. This loop should run from 1 - LimitX, outputting the asterix.
- You will also need to check to see if you have reached LimitX so you can output a newline character / line break / whatever.
I don't know Perl syntax, but the below (PHP) should offer some visible logic. -
// Amount of columns and rows.
-
$limitX = 7;
-
$limitY = 10;
-
$out = '*';
-
-
// Loop through the columns.
-
for( $y = 1; $y <= $limitY; ++$y )
-
{
-
// Inner loop for rows.
-
for ( $x = 1; $x <= $limitX; ++$x )
-
{
-
// Check to see if we are at the end of the row and need a newline char (\n)
-
if ( $x === $limitX )
-
{
-
echo "*\n";
-
}
-
else
-
{
-
echo "*";
-
}
-
}
-
}
-
Hope this helps.
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: Integer Repetition
That can be simplified: - my $x = 7;
-
my $y = 7;
-
for (1..$x) {
-
print "*" x $y, "\n";
-
}
Could also look into format() or perlform: http://perldoc.perl.org/perlform.html |  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | re: Integer Repetition Quote:
Originally Posted by KevinADC That can be simplified: - my $x = 7;
-
my $y = 7;
-
for (1..$x) {
-
print "*" x $y, "\n";
-
}
Could also look into format() or perlform: http://perldoc.perl.org/perlform.html Sorry to go off-topic, but what does the 'x' operator do?
|  | Moderator | | Join Date: Jul 2007 Location: Arkansas
Posts: 900
| | | re: Integer Repetition Quote:
Originally Posted by Markus Sorry to go off-topic, but what does the 'x' operator do? From the perl docs. Quote:
Binary "x" is the repetition operator. In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list. If the right operand is zero or negative, it returns an empty string or an empty list, depending on the context.
--Kevin
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: Integer Repetition
Yes, 'x' is a perl operator, don't know if PHP has something similar.
| | Familiar Sight | | Join Date: Nov 2006
Posts: 168
| | | re: Integer Repetition Quote:
Originally Posted by KevinADC That can be simplified: - my $x = 7;
-
my $y = 7;
-
for (1..$x) {
-
print "*" x $y, "\n";
-
}
Could also look into format() or perlform: http://perldoc.perl.org/perlform.html Thanks for all the replies so far, very beneficial.
Kevin, why do you use 'my'?
your above code prints out a 7x7 square which is cool, but if you wanted to print a hollow square? just the asterisks on the outline of the square?
sorry its probably my fault for not making it very clear..
regards
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: Integer Repetition
I use "my" because I write all my perls script using "strict", so using "my" is a habit, a good one. As far as a hollow square, you have already been given enough information to figure that out. Try something, if you get stuck, post your code,
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|