dynamic sorting

  • Thread starter Janek Schleicher
  • Start date
J

Janek Schleicher

ARAVIND wrote at Sat, 05 Jul 2003 04:11:57 -0700:
say i have a variable
$temp = "ARAVIND 25 BANGALORE";
I want just ARAVIND and BANGALORE
if i do
@splitval = split /\d /,$temp;
then i get
$splitval[0] = 'ARAVIND 25';
$splitval[1] = 'BANGALORE';

I doubt it. I get
$splitval[0] eq "ARAVIND 2" and
$splitval[1] eq " BANGALORE"
i can further split it from $splitval[0] and get only ARAVIND.
but how to make it one shot....spliting.

Well you could do instead a
split /\s*\d+\s*/, $temp;
# or
split /[\s\d]+/, $temp;

But you also could solve it with a regexp:

my @splitval = $temp =~ /(\w+)/g;


Greetings,
Janek
 
A

ARAVIND

hello,

say i have a variable
$temp = "ARAVIND 25 BANGALORE";
I want just ARAVIND and BANGALORE
if i do
@splitval = split /\d /,$temp;
then i get
$splitval[0] = 'ARAVIND 25';
$splitval[1] = 'BANGALORE';

i can further split it from $splitval[0] and get only ARAVIND.
but how to make it one shot....spliting.

Regards,
Aravind.
 
B

bd

hello,

say i have a variable
$temp = "ARAVIND 25 BANGALORE";
I want just ARAVIND and BANGALORE
if i do
@splitval = split /\d /,$temp;
then i get
$splitval[0] = 'ARAVIND 25';
$splitval[1] = 'BANGALORE';

i can further split it from $splitval[0] and get only ARAVIND.
but how to make it one shot....spliting.

Regards,
Aravind.

Use a regex:
$temp =~ s/^(\W+)\s*\d+\s*(\W+)$/;
@splitval = ($1, $2); # @splitval = ('ARAVIND', 'BANGALORE')

Read perldoc perlre for more info on regexes.
 
J

John W. Krahn

bd said:
Use a regex:
$temp =~ s/^(\W+)\s*\d+\s*(\W+)$/;

The \W character class will not match either 'ARAVIND' or 'BANGALORE'.
You probably meant to use the \w character class.

@splitval = ($1, $2); # @splitval = ('ARAVIND', 'BANGALORE')

You shouldn't use the values of $1 and $2 unless you know the match was
successful.


John
 
M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
hello,

say i have a variable
$temp = "ARAVIND 25 BANGALORE";
I want just ARAVIND and BANGALORE
if i do
@splitval = split /\d /,$temp;
then i get
$splitval[0] = 'ARAVIND 25';
$splitval[1] = 'BANGALORE';

i can further split it from $splitval[0] and get only ARAVIND.
but how to make it one shot....spliting.

Your logical delimiter is whitespace(s), not the number. Use that as the split token:

$temp = "ARAVIND 25 BANGALORE";
@words = split(/\s+/, $temp);
print "First is $words[0] and last is $words[-1]\n";

Or you could do it without the intermediate @words array:

$temp = "ARAVIND 25 BANGALORE";
($first, $last) = (split(/\s+/, $temp))[0,-1];
print "First is $first and last is $last\n";

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/B03VeS99pGMif6wRAsd5AKDGgo1o9LYa6xO660+k/QAcPBonkgCfRVPh
Jd8JHbCt1rRQrl/NPaBKxas=
=LKhA
-----END PGP SIGNATURE-----
 
R

Rohan Romanus Almeida

ARAVIND said:
say i have a variable
$temp = "ARAVIND 25 BANGALORE";
@splitval = split /\d /,$temp;
then i get
$splitval[0] = 'ARAVIND 25';

I think the OP has not tested
the demo program well enough.

If all goes well, $splitval[0] should
contain "ARAVIND 2", since the
split will use "5 " as the delimiter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top