in reply to (Ovid) Re: Transpose some spaces to commas in a string
in thread Transpose some spaces to commas in a string

Thats a problem, some user names are Bob Q Smith, Jane Doe, Ralph Dilbet Johnson, only was I see to do it is to detect the 4 "0000" in that serial number then jump back past the previous word and replace that space with a comma. I have no clue on how to do that. Any tips?
  • Comment on RE: (Ovid) Re: Transpose some spaces to commas in a string

Replies are listed 'Best First'.
RE: RE: (Ovid) Re: Transpose some spaces to commas in a string
by swiftone (Curate) on Sep 29, 2000 at 21:14 UTC
    Offhand, I'd say try the FAQ about putting commas into a number. If the variable mess is at the start of your string, reverse it.
    Bob Q Smith bsmith 00001234567 5/1/00 12:00:00
    becomes
    00:00:21 00/1/5 76543210000 htimsb htimS Q boB

    And a fairly simple regex can insert your commas. Re-reverse your result, and there you go.

    Update: here's my attempt that appears to do what you want:

    #!/usr/bin/perl -w use strict; my $test="Bob Q Smith bsmith 00001234567 5/1/00 12:00:00"; $test = reverse $test; #documenting the simple but nasty looking regex is left as an exercise + for the reader $test=~s/([:\d]+)\s([\d\/]+)\s([\d]+)\s([\w]+)\s(.*)/$1,$2,$3,$4,$5/ o +r die "Pattern didn't match: $test"; $test = reverse $test; print $test;
(Ovid) RE(3): Transpose some spaces to commas in a string
by Ovid (Cardinal) on Sep 29, 2000 at 21:07 UTC
    Untested:
    my $testvar = "Bob Q Smith bsmith 00001234567 5/1/00 12:00:00"; $testvar =~ s/ \s # a whitespace character (space, tab, etc) ( # Capture to $1 \w+ # one or more word characters (e.g. bsmi +th) ) \s # a whitespace character ( # Capture to $2 \d+ # one or more digits ) \s # a whitespace character /,$1,$2,/x;
    If your data is relatively clean, I think that should work. It relies on you knowing that the first digits you encounter are going to be your serial number and immediately preceding them are some letters. From there, it's pretty straightforward. There are many different ways to write this regex and this may not be the best, but it's fairly clear.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

      Nice one as that works on names with zero or more middle initials, and isn't bothered by capitalized usernames either (as my quick attempt was).
RE: RE: (Ovid) Re: Transpose some spaces to commas in a string
by jptxs (Curate) on Sep 29, 2000 at 21:09 UTC

    Than the first thing you need to do is write something to clean up the data. Maybe throw the cleaned data into and array, run the regex on that and then output those results to a file...

    Maybe if you could change the input so it comes as something like:

    Bob Q Smith 00001234567 bsmith 5/1/00 12:00:00

    It would make it much easier then to use regexes to catch the spaces that fall between number and letter fields. I think this goes under the "it would be easier to control the input if you can" department.

    -- I'm a solipsist, and so is everyone else. (think about it)