grouping with modulus

Discussion in 'Perl Misc' started by Marko, Apr 17, 2007.

  1. Marko

    Marko Guest

    First of all, sorry for bad english.

    I have the following problem, that can't just seem to get work...
    I have a string of numbers that can be from 4 to 20 numbers long. I need
    to print this string grouped by 5 numbers and counted by from last to first.
    For example:
    String = 462345678474
    I need = 46 23456 78474
    And second example:
    String = 4623456784
    I need = 46234 56784

    I have done the following code, but i dosn't work if the string modulus
    five is zero.
    Hope you understand better, when loking code...

    my $string = "462345678474";
    my @array = ();
    foreach my $n (split(//, $viite)) {
    unshift @array, $n;
    }
    my $something;
    for (my $i = length($string) - 1, my $m = 1; $i >= 0; $i--, $m++) {
    if ($m % 5 == 0) {
    $viitenro .= " ";
    }
    $viitenro .= $array[$i];
    }
    print $something ."\n";

    Thanks for advance,

    --
    Marko
    Marko, Apr 17, 2007
    #1
    1. Advertising

  2. Marko

    Guest

    On Apr 16, 8:03 pm, Marko <> wrote:
    > First of all, sorry for bad english.
    >
    > I have the following problem, that can't just seem to get work...
    > I have a string of numbers that can be from 4 to 20 numbers long. I need
    > to print this string grouped by 5 numbers and counted by from last to first.
    > For example:
    > String = 462345678474
    > I need = 46 23456 78474
    > And second example:
    > String = 4623456784
    > I need = 46234 56784
    >



    In this case I would adapt the solution from:

    $ perldoc -q "How can I output my numbers with commas added"

    That is:

    my $string = "462345678474";
    print $string, "\n";

    1 while $string =~ s/(\d+)(\d{5})/$1 $2/;
    print $string, "\n";

    --
    Hope this helps,
    Steven
    , Apr 17, 2007
    #2
    1. Advertising

  3. Marko

    Marko Guest

    kirjoitti:
    > On Apr 16, 8:03 pm, Marko <> wrote:
    >> First of all, sorry for bad english.
    >>
    >> I have the following problem, that can't just seem to get work...
    >> I have a string of numbers that can be from 4 to 20 numbers long. I need
    >> to print this string grouped by 5 numbers and counted by from last to first.
    >> For example:
    >> String = 462345678474
    >> I need = 46 23456 78474
    >> And second example:
    >> String = 4623456784
    >> I need = 46234 56784

    >
    > In this case I would adapt the solution from:
    >
    > $ perldoc -q "How can I output my numbers with commas added"
    >
    > That is:
    >
    > my $string = "462345678474";
    > print $string, "\n";
    >
    > 1 while $string =~ s/(\d+)(\d{5})/$1 $2/;
    > print $string, "\n";


    Worked perfectly as i wanted!
    Thank you very much for help.

    --
    Marko
    Marko, Apr 17, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Andr=E9s?=

    Modulus 12

    =?ISO-8859-1?Q?Andr=E9s?=, Feb 14, 2005, in forum: VHDL
    Replies:
    4
    Views:
    3,843
    Tuukka Toivonen
    Feb 14, 2005
  2. Tasperian Jigs

    Problems with floating point and modulus

    Tasperian Jigs, Apr 18, 2006, in forum: Java
    Replies:
    2
    Views:
    2,109
    Patricia Shanahan
    Apr 18, 2006
  3. Chris Watson

    Need precision using modulus

    Chris Watson, Aug 8, 2003, in forum: C++
    Replies:
    6
    Views:
    438
    yvan joffre
    Aug 8, 2003
  4. bug in modulus?

    , Apr 23, 2006, in forum: Python
    Replies:
    10
    Views:
    641
    Christophe
    May 3, 2006
  5. priya

    MODULUS operator

    priya, Nov 15, 2006, in forum: VHDL
    Replies:
    3
    Views:
    9,862
    priya
    Nov 16, 2006
Loading...

Share This Page