From 7da798d8fa2d19d09c87390efc5dde02b8ad267c Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Sat, 8 Jan 2022 02:41:03 -0600 Subject: [PATCH] perl/commas.pl: comma-ize numbers greater than 999 --- perl/commas.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100755 perl/commas.pl diff --git a/perl/commas.pl b/perl/commas.pl new file mode 100755 index 0000000..17ab63a --- /dev/null +++ b/perl/commas.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +foreach $arg (0..$#ARGV) { + my $num = int($ARGV[$arg]); + my $fmt = "", $cnt = 0; + + while($num > 0) { + $fmt = ($num % 10) . $fmt; + + if(++$cnt % 3 == 0 and $num >= 10) { + $fmt = "," . $fmt; + } + + use integer; + $num /= 10; + } + + print "$fmt\n"; +}