By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,675 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

Integer Repetition

100+
P: 173
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
Feb 25 '09 #1
Share this Question
Share on Google+
15 Replies


KevinADC
Expert 2.5K+
P: 4,059
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.
Feb 25 '09 #2

KevinADC
Expert 2.5K+
P: 4,059
hehehe.... seems that the thought of having to write this very simple code his/her self was a bit too much to contemplate.
Feb 26 '09 #3

100+
P: 173
KevinADC,

sorry mate for the late reply, well busy.

ok i think ive cracked it with 5digits:

Expand|Select|Wrap|Line Numbers
  1. print "please insert a 5-digit integer: \n";
  2.  
  3. $string = <STDIN>;
  4.  
  5. $var = substr($string,0,1) x 1;
  6. $var2 = substr($string,1,1) x 2;
  7. $var3 = substr($string,2,1) x 3;
  8. $var4 = substr($string,3,1) x 4;
  9. $var5 = substr($string,4,1) x 5;
  10.  
  11. 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
Feb 26 '09 #4

nithinpes
Expert 100+
P: 410
To reduce the number of lines, you could have used for loop:
Expand|Select|Wrap|Line Numbers
  1. print "please insert a 5-digit integer: \n"; 
  2.  
  3. $string = <STDIN>; 
  4. for($i=1;$i<=5;$i++) {  
  5. my $var = substr($string,$i-1,1) x $i; 
  6. print "$var\n";
  7. }
  8.  
Another way of doing it would be :
Expand|Select|Wrap|Line Numbers
  1. print "please insert a 5-digit integer: \n"; 
  2. $string = <STDIN>; 
  3. $i=1;
  4. print $1x$i++."\n" while($string=~/(\d)/g); #works for any no. of digits
  5.  
Feb 26 '09 #5

BeemerBiker
P: 87
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".
Feb 26 '09 #6

100+
P: 173
@nithinpes

Can you please provide me some explaination for the above code?

thanks
Feb 26 '09 #7

KevinADC
Expert 2.5K+
P: 4,059
@Beany
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.
Feb 26 '09 #8

100+
P: 173
@KevinADC
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
Feb 27 '09 #9

Markus
Expert 5K+
P: 6,050
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:
  1. 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.
  2. 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.
  3. 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.
  4. 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.
Expand|Select|Wrap|Line Numbers
  1.         // Amount of columns and rows.
  2.     $limitX = 7;
  3.     $limitY = 10;
  4.     $out    = '*';
  5.  
  6.     // Loop through the columns.
  7.     for( $y = 1; $y <= $limitY; ++$y )
  8.     {
  9.         // Inner loop for rows.
  10.         for ( $x = 1; $x <= $limitX; ++$x )
  11.         {
  12.             // Check to see if we are at the end of the row and need a newline char (\n)
  13.             if ( $x === $limitX )
  14.             {
  15.                 echo "*\n";
  16.             }
  17.             else
  18.             {
  19.                 echo "*";
  20.             }
  21.         }
  22.     }
  23.  
Hope this helps.
Mar 2 '09 #10

KevinADC
Expert 2.5K+
P: 4,059
That can be simplified:

Expand|Select|Wrap|Line Numbers
  1. my $x = 7;
  2. my $y = 7;
  3. for (1..$x) {
  4.     print "*" x $y, "\n";
  5. }
Could also look into format() or perlform:

http://perldoc.perl.org/perlform.html
Mar 2 '09 #11

Markus
Expert 5K+
P: 6,050
@KevinADC
Sorry to go off-topic, but what does the 'x' operator do?
Mar 2 '09 #12

eWish
Expert 100+
P: 971
@Markus
From the perl docs.

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
Mar 2 '09 #13

KevinADC
Expert 2.5K+
P: 4,059
Yes, 'x' is a perl operator, don't know if PHP has something similar.
Mar 3 '09 #14

100+
P: 173
@KevinADC
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
Mar 3 '09 #15

KevinADC
Expert 2.5K+
P: 4,059
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,
Mar 3 '09 #16

Post your reply

Sign in to post your reply or Sign up for a free account.