Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split problem [A-Z] 1

Status
Not open for further replies.

PerlElvir

Technical User
Aug 23, 2005
68
Hi all , I want to split something by capital latter and I have this situation:

$var="Sam 134 Dan 234 Hal 345";

@tax1 = split(/[A-Z]/, $var);

now in array I have

@tax1=("am 134","an 234","al 345")

my question is how can I split by capital latter, but also to have that capital letter, so I need to have like this:

@tax1=("Sam 134","Dan 234","Hal 345")
 
Don't know how accurate your sample $var is of your real data but an alternative to split is using a regexp to return a list:

Code:
my $var="Sam 134 Dan 234 Hal 345";
my @tax1 = $var =~ /([A-Z]\w+ \d+)/g;
print map {"$_\n"} @tax1;
 
I know that there is a performance hit by using it, but if you want to use split you can use positive lookahead assertion - this works with the example you provided.

Code:
my $str = 'Sam 134 Dan 234 Hal 345';
my @tax1 = split /\s*(?=[A-Z])\s*/, $str;
print "\|$_\|\n" for @tax1;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top