Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Split confusion

by perlfan (Vicar)
on Jun 03, 2020 at 15:21 UTC ( [id://11117658]=note: print w/replies, xml ) Need Help??


in reply to Split confusion

I'm getting back to Perl after about a decade

Welcome back!

use strict; use warnings; use Data::Dumper (); my $name = "SMITH-JONES"; my @temp = split(/(-| )/,$name); print Data::Dumper::Dumper(\@temp); $name = "SMITH JONES"; @temp = split(/(-| )/,$name); print Data::Dumper::Dumper(\@temp);
Output:
$VAR1 = [ 'SMITH', '-', 'JONES' ]; $VAR1 = [ 'SMITH', ' ', 'JONES' ];
Seems to work, are you sure you have an actual space on the RHS of the |?

Also, seems like you could be kind to your computer and not save the capture results:

my @temp = split(/(?:-| )/,$name);
Final suggestion is to use the exact same regex in the if as you do in the split.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (None)
    As of 2024-04-25 00:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found