Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Extracting names from string

by dimar (Curate)
on Feb 08, 2005 at 16:08 UTC ( [id://429101]=note: print w/replies, xml ) Need Help??


in reply to Extracting names from string

One way to do it.
$string = "s005219/Doe John|John.Doe\\"; @array = split/[^\w\s\.]+/, $string; print join "--",@array;
Assumptions:
You care about separating the strings on delimiters.
Anything that isn't a space, alphanumeric, underscore, or period is a delimiter.

How to get "Doe John" out of the array is left as an exercise for the reader.

Replies are listed 'Best First'.
Re^2: Extracting names from string
by Not_a_Number (Prior) on Feb 08, 2005 at 17:30 UTC

    The trouble is, names can contain other things than spaces and alphanumerics (as defined by your locale).

    Hyphens, for example. Apostrophes. Accented characters.

    Best to split on what you know (the slash and the pipe) than what you can only guess at.

    dave

      A valid consideration. In fact you will notice I added the 'dot' (in addition to alphanumerics) simply because one was in the sample provided by the OP. The (simplifying) assumptions I enumerated were guesstimates.

      If we *really* wanted to be precise, we'd want both a spec and input validation (to know what flavor of xSV we were dealing with here, and to ensure user-data was 'scrubbed' before ever getting into the file/database/whatever to begin with)

      (for example, what happens in the alien case where you get a user who has a pipe or a slash in their name ... hmm ... isn't *slash* the name of some rock band dude ... I digress).

      You should know exactly what character sequences are in your delimiter-space, and what characters are in your message-space, and the two sets should be both finite and orthogonal.

      It's always a good reminder that (at least in production code) there should be none of this such 'guesswork' involved at all. ... but close enough for a tutorial on 'split'...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://429101]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found