grouping with modulus

M

Marko

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,
 
A

attn.steven.kuo

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";
 
M

Marko

(e-mail address removed) kirjoitti:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top