Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Multiplication table

by jinx (Initiate)
on Aug 01, 2003 at 15:14 UTC ( #279996=perlquestion: print w/replies, xml ) Need Help??

jinx has asked for the wisdom of the Perl Monks concerning the following question:

So after seeing most of the questions here my question isnt worthy of this web page but im gonna give it a shot any way. I just started doing perl. DUH! NEWAY I didnt sleep last night because im still working with this. There is some unnesseseary stuff in it because i started to move things around well "I was guessing" HELP

#!/usr/bin/perl print "How many would you like to calc?\nEX. 10, 100, 1000 :" ; $howmany = <STDIN>; $counter = 0; $counting = 0; $count = 0; while ($howmany > $counter ) { $counter++; $counting++; if ($counter == 10) { $counter == 0; } $answer = $counting * $count; $stuff = "|".$count." * ".$counting." = ".$answer."|"; if ($count < 10) { print $stuff; $count++; } if ($count == 10) { print $stuff."\n"; $stuff = ""; $counting == 0; $count == 1; } }

if $howmany = 50 THEN I NEED IT TO LOOK something LIKE THIS

#|1|*|1|=|1|2|*|1|=|2|3|*|1|=|3|4|*|1|=|4|5|*|1|=|5|6|*|1|=|6|7|*|1|=|7|8|*|1|=|8|9|*|1|=|9|10|*|1|=|10 #|2|*|2|=|4|3|*|2|=|6|4|*|2|=|8|5|*|2|=|10|6|*|2|=|12|7|*|2|=|14|8|*|2|=|16|9|*|2|=|18|10|*|2|=|20 #|2|*|3|=|6|3|*|3|=|9|4|*|3|=|12|5|*|3|=|15|6|*|3|=|18|7|*|3|=|21|8|*|3|=|24|9|*|3|=|27|10|*|3|=|30 #|2|*|4|=|8|3|*|4|=|12|4|*|4|=|16|5|*|4|=|20|6|*|4|=|24|7|*|4|=|28|8|*|4|=|32|9|*|4|=|36|10|*|4|=|40 #|2|*|5|=|10|3|*|5|=|15|4|*|5|=|20|5|*|5|=|25|6|*|5|=|30|7|*|5|=|35|8|*|5|=|40|9|*|5|=|45|10|*|5|=|50 #|2|*|6|=|12|3|*|6|=|18|4|*|6|=|24|5|*|6|=|30|6|*|6|=|36|7|*|6|=|42|8|*|6|=|48|9|*|6|=|54|10|*|6|=|60 #|2|*|7|=|14|3|*|7|=|21|4|*|7|=|28|5|*|7|=|35|6|*|7|=|42|7|*|7|=|49|8|*|7|=|56|9|*|7|=|63|10|*|7|=|70 #|2|*|8|=|16|3|*|8|=|24|4|*|8|=|32|5|*|8|=|40|6|*|8|=|48|7|*|8|=|56|8|*|8|=|64|9|*|8|=|72|10|*|8|=|80 #|2|*|9|=|18|3|*|9|=|27|4|*|9|=|36|5|*|9|=|45|6|*|9|=|54|7|*|9|=|63|8|*|9|=|72|9|*|9|=|81|10|*|9|=|90 #|2|*|10|=|20|3|*|10|=|30|4|*|10|=|40|5|*|10|=|50|6|*|10|=|60|7|*|10|=|70|8|*|10|=|80|9|*|10|=|90|10|*|10|=|100 #|2|*|11|=|22|3|*|11|=|33|4|*|11|=|44|5|*|11|=|55|6|*|11|=|66|7|*|11|=|77|8|*|11|=|88|9|*|11|=|99|10|*|11|=|110 #|2|*|12|=|24|3|*|12|=|36|4|*|12|=|48|5|*|12|=|60|6|*|12|=|72|7|*|12|=|84|8|*|12|=|96|9|*|12|=|108|10|*|12|=|120 #|2|*|13|=|26|3|*|13|=|39|4|*|13|=|52|5|*|13|=|65|6|*|13|=|78|7|*|13|=|91|8|*|13|=|104|9|*|13|=|117|10|*|13|=|130 #|2|*|14|=|28|3|*|14|=|42|4|*|14|=|56|5|*|14|=|70|6|*|14|=|84|7|*|14|=|98|8|*|14|=|112|9|*|14|=|126|10|*|14|=|140 #|2|*|15|=|30|3|*|15|=|45|4|*|15|=|60|5|*|15|=|75|6|*|15|=|90|7|*|15|=|105|8|*|15|=|120|9|*|15|=|135|10|*|15|=|150 #|2|*|16|=|32|3|*|16|=|48|4|*|16|=|64|5|*|16|=|80|6|*|16|=|96|7|*|16|=|112|8|*|16|=|128|9|*|16|=|144|10|*|16|=|160 #|2|*|17|=|34|3|*|17|=|51|4|*|17|=|68|5|*|17|=|85|6|*|17|=|102|7|*|17|=|119|8|*|17|=|136|9|*|17|=|153|10|*|17|=|170 #|2|*|18|=|36|3|*|18|=|54|4|*|18|=|72|5|*|18|=|90|6|*|18|=|108|7|*|18|=|126|8|*|18|=|144|9|*|18|=|162|10|*|18|=|180 #|2|*|19|=|38|3|*|19|=|57|4|*|19|=|76|5|*|19|=|95|6|*|19|=|114|7|*|19|=|133|8|*|19|=|152|9|*|19|=|171|10|*|19|=|190 #|2|*|20|=|40|3|*|20|=|60|4|*|20|=|80|5|*|20|=|100|6|*|20|=|120|7|*|20|=|140|8|*|20|=|160|9|*|20|=|180|10|*|20|=|200 #|2|*|21|=|42|3|*|21|=|63|4|*|21|=|84|5|*|21|=|105|6|*|21|=|126|7|*|21|=|147|8|*|21|=|168|9|*|21|=|189|10|*|21|=|210 #|2|*|22|=|44|3|*|22|=|66|4|*|22|=|88|5|*|22|=|110|6|*|22|=|132|7|*|22|=|154|8|*|22|=|176|9|*|22|=|198|10|*|22|=|220 #|2|*|23|=|46|3|*|23|=|69|4|*|23|=|92|5|*|23|=|115|6|*|23|=|138|7|*|23|=|161|8|*|23|=|184|9|*|23|=|207|10|*|23|=|230 #|2|*|24|=|48|3|*|24|=|72|4|*|24|=|96|5|*|24|=|120|6|*|24|=|144|7|*|24|=|168|8|*|24|=|192|9|*|24|=|216|10|*|24|=|240 #|2|*|25|=|50|3|*|25|=|75|4|*|25|=|100|5|*|25|=|125|6|*|25|=|150|7|*|25|=|175|8|*|25|=|200|9|*|25|=|225|10|*|25|=|250 #|2|*|26|=|52|3|*|26|=|78|4|*|26|=|104|5|*|26|=|130|6|*|26|=|156|7|*|26|=|182|8|*|26|=|208|9|*|26|=|234|10|*|26|=|260 #|2|*|27|=|54|3|*|27|=|81|4|*|27|=|108|5|*|27|=|135|6|*|27|=|162|7|*|27|=|189|8|*|27|=|216|9|*|27|=|243|10|*|27|=|270 #|2|*|28|=|56|3|*|28|=|84|4|*|28|=|112|5|*|28|=|140|6|*|28|=|168|7|*|28|=|196|8|*|28|=|224|9|*|28|=|252|10|*|28|=|280 #|2|*|29|=|58|3|*|29|=|87|4|*|29|=|116|5|*|29|=|145|6|*|29|=|174|7|*|29|=|203|8|*|29|=|232|9|*|29|=|261|10|*|29|=|290 #|2|*|30|=|60|3|*|30|=|90|4|*|30|=|120|5|*|30|=|150|6|*|30|=|180|7|*|30|=|210|8|*|30|=|240|9|*|30|=|270|10|*|30|=|300 #|2|*|31|=|62|3|*|31|=|93|4|*|31|=|124|5|*|31|=|155|6|*|31|=|186|7|*|31|=|217|8|*|31|=|248|9|*|31|=|279|10|*|31|=|310 #|2|*|32|=|64|3|*|32|=|96|4|*|32|=|128|5|*|32|=|160|6|*|32|=|192|7|*|32|=|224|8|*|32|=|256|9|*|32|=|288|10|*|32|=|320 #|2|*|33|=|66|3|*|33|=|99|4|*|33|=|132|5|*|33|=|165|6|*|33|=|198|7|*|33|=|231|8|*|33|=|264|9|*|33|=|297|10|*|33|=|330 #|2|*|34|=|68|3|*|34|=|102|4|*|34|=|136|5|*|34|=|170|6|*|34|=|204|7|*|34|=|238|8|*|34|=|272|9|*|34|=|306|10|*|34|=|340 #|2|*|35|=|70|3|*|35|=|105|4|*|35|=|140|5|*|35|=|175|6|*|35|=|210|7|*|35|=|245|8|*|35|=|280|9|*|35|=|315|10|*|35|=|350 #|2|*|36|=|72|3|*|36|=|108|4|*|36|=|144|5|*|36|=|180|6|*|36|=|216|7|*|36|=|252|8|*|36|=|288|9|*|36|=|324|10|*|36|=|360 #|2|*|37|=|74|3|*|37|=|111|4|*|37|=|148|5|*|37|=|185|6|*|37|=|222|7|*|37|=|259|8|*|37|=|296|9|*|37|=|333|10|*|37|=|370 #|2|*|38|=|76|3|*|38|=|114|4|*|38|=|152|5|*|38|=|190|6|*|38|=|228|7|*|38|=|266|8|*|38|=|304|9|*|38|=|342|10|*|38|=|380 #|2|*|39|=|78|3|*|39|=|117|4|*|39|=|156|5|*|39|=|195|6|*|39|=|234|7|*|39|=|273|8|*|39|=|312|9|*|39|=|351|10|*|39|=|390 #|2|*|40|=|80|3|*|40|=|120|4|*|40|=|160|5|*|40|=|200|6|*|40|=|240|7|*|40|=|280|8|*|40|=|320|9|*|40|=|360|10|*|40|=|400 #|2|*|41|=|82|3|*|41|=|123|4|*|41|=|164|5|*|41|=|205|6|*|41|=|246|7|*|41|=|287|8|*|41|=|328|9|*|41|=|369|10|*|41|=|410 #|2|*|42|=|84|3|*|42|=|126|4|*|42|=|168|5|*|42|=|210|6|*|42|=|252|7|*|42|=|294|8|*|42|=|336|9|*|42|=|378|10|*|42|=|420 #|2|*|43|=|86|3|*|43|=|129|4|*|43|=|172|5|*|43|=|215|6|*|43|=|258|7|*|43|=|301|8|*|43|=|344|9|*|43|=|387|10|*|43|=|430 #|2|*|44|=|88|3|*|44|=|132|4|*|44|=|176|5|*|44|=|220|6|*|44|=|264|7|*|44|=|308|8|*|44|=|352|9|*|44|=|396|10|*|44|=|440 #|2|*|45|=|90|3|*|45|=|135|4|*|45|=|180|5|*|45|=|225|6|*|45|=|270|7|*|45|=|315|8|*|45|=|360|9|*|45|=|405|10|*|45|=|450 #|2|*|46|=|92|3|*|46|=|138|4|*|46|=|184|5|*|46|=|230|6|*|46|=|276|7|*|46|=|322|8|*|46|=|368|9|*|46|=|414|10|*|46|=|460 #|2|*|47|=|94|3|*|47|=|141|4|*|47|=|188|5|*|47|=|235|6|*|47|=|282|7|*|47|=|329|8|*|47|=|376|9|*|47|=|423|10|*|47|=|470 #|2|*|48|=|96|3|*|48|=|144|4|*|48|=|192|5|*|48|=|240|6|*|48|=|288|7|*|48|=|336|8|*|48|=|384|9|*|48|=|432|10|*|48|=|480 #|2|*|49|=|98|3|*|49|=|147|4|*|49|=|196|5|*|49|=|245|6|*|49|=|294|7|*|49|=|343|8|*|49|=|392|9|*|49|=|441|10|*|49|=|490

and so on... can you help

edited by ybiC: reformat to reduce render horkage, retitle from "Me no good at Xing anything"

Replies are listed 'Best First'.
Re: Me no good at Xing anything
by pzbagel (Chaplain) on Aug 01, 2003 at 15:35 UTC

    Wow, that's a lot of output and next to no description of your problem. But I think you are trying to print out multiplication tables. You almost have it, however, your problem is that you are incrementing both of your counters simultaneously. In fact $counter and $counting are incremented one after the other. That leads to your problem where you get 0*1, then 1*2, etc. What you want is a nested loop. That is for each $counter, you want to iterate through $counting 10 times to print out the times table for that number, then print the newline, increment $counter, and continue. Here's a basic example with for loops(They're your friend)

    for my $x (1..$howmany) { print "#"; for my $y (1..10) { my $answer = $x * $y; print "|$x|*|$y|=|$answer"; } print "\n"; }

    Notice how one for loop is nested in the other, so for every iteration of $x which goes from 1 through $howmany, you get 10 iterations of $y. performing the math and printing inside the second for loop and you are done. No fuss, no muss, and no tracking of several counter variables.

    HTH

Re: Me no good at Xing anything
by Cine (Friar) on Aug 01, 2003 at 15:45 UTC
    Next time, please be more specific about what your exact problem is, instead of letting us guessing it.
    Also give a good title next time, this one doesnt say one thing about what your problem is either. Try with newbie problem with <whatever it is you have a problem with>...
    #!/usr/bin/perl use strict; #Enable stuff to prevent you from doing many stupid things use warnings; #give me warnings when i do make stupid things, that are + not that stupid. print "How many would you like to calc?\nEX. 10, 100, 1000 :" ; my $howmany = int <STDIN>; #convert the read in to a number, we dont a +ctually check it, but if its not a number, we read 0 for my $outer (1..$howmany) { for (2 .. 10) { #alias the number to $_ my $answer = $_ * $outer; print "|$_ * $outer = $answer|"; } print "\n"; }


    T I M T O W T D I
      Me no good at Xing anything Just ment that Im not Xucking good and anything It also ment that I am not good at Multiplying (2 X 2 = 3) Or spelling - Syntax How ever you are God and I am very Sorry.... I will forever be in my place as one of your simpiletions Thank you again.. Jinx
        That dope sounds good, can you share?

        T I M T O W T D I
(jeffa) Re: Multiplication table
by jeffa (Bishop) on Aug 02, 2003 at 14:35 UTC
    Here is how i would do it. Don't print out the values as you calculate them - instead, store them in a 2-D array:
    sub get_table { my $max = $_[0] - 1; my @table; for my $y (0..$max) { for my $x (0..$max) { $table[$y][$x] = ($x + 1) * ($y + 1); } } return @table; }
    so that you can use existing CPAN modules to render them. For example, via Text::Table:
    use Text::Table; my $max = shift || 12; my @table = get_table($max); my $text_table = Text::Table->new(1..$max); $text_table->load(@table); print $text_table->body;
    or an XHTML table via DBIx::XHTML_Table:
    use DBIx::XHTML_Table; my $max = shift || 12; print DBIx::XHTML_Table ->new([get_table($max)],[]) ->output({no_head=>1}) ;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2022-10-03 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (13 votes). Check out past polls.

    Notices?