Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Transform numeric string in numeric string

by spadacciniweb (Deacon)
on Nov 28, 2005 at 16:42 UTC ( #512263=perlquestion: print w/replies, xml ) Need Help??
spadacciniweb has asked for the wisdom of the Perl Monks concerning the following question:

I would to change a number in an other number as in example:

123456789 -> 123.456.789

Rember I: the input number length can to be whichever.
Remember II: i would to build a regular expression that make this function. I don't want a script as:
while { ... }
but I want a unique line.

Replies are listed 'Best First'.
Re: Transform numeric string in numeric string
by Fletch (Chancellor) on Nov 28, 2005 at 16:48 UTC

    See perldoc -q "commas added" and adapt.

Re: Transform numeric string in numeric string
by ikegami (Pope) on Nov 28, 2005 at 16:47 UTC

    What happens if you have 8 characters instead of 9?


    $num =~ s/(.{3})(?!$)/$1./g;

    12.345.678 (efficient):

    $num = reverse $num; $num =~ s/(.{3})(?!$)/$1./g; $num = reverse $num;

    12.345.678 using just a regexp (inefficient):

    $num =~ s/(?!^)(.{3})(?=(?:.{3})*$)/.$1/g;
      it's ok:
      $num =~ s/(?!^)(.{3})(?=(?:.{3})*$)/.$1/g;
      thank you!!
Re: Transform numeric string in numeric string
by davorg (Chancellor) on Nov 28, 2005 at 16:59 UTC
Re: Transform numeric string in numeric string
by kulls (Hermit) on Nov 29, 2005 at 03:39 UTC
    Try this formatting module for string to number and vice versa.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://512263]
Approved by ww
belg4mit kicks Perl in the shins, "Unmatched ) in regex; marked by" my ass
[belg4mit]: Nothing wrong with perl -le 'print ":)" =~ /(?^u:^(\:|\:\))$) /'
[Lotus1]: belg4mit are you on a Linix server?
[belg4mit]: yes. the regular expression is generated in code from a text file. that s the regular expression that Perl blows up when it reaches it, but if I use it from the comman line it's fine
[belg4mit]: oh, wait, damn, different lne number
[choroba]: same Perl version?
[choroba]: oh, ok
[belg4mit]: I had \Q'd in one RE but not a subsequent one, doh

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-04-25 16:40 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (460 votes). Check out past polls.