Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   dynamic sorting (http://www.velocityreviews.com/forums/t869947-dynamic-sorting.html)

Janek Schleicher 07-05-2003 07:27 AM

Re: dynamic sorting
 
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

ARAVIND 07-05-2003 11:11 AM

dynamic sorting
 
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.

Tad McClellan 07-05-2003 02:36 PM

Re: dynamic sorting
 
ARAVIND <arawind@yahoo.com> wrote:

> say i have a variable
> $temp = "ARAVIND 25 BANGALORE";


> @splitval = split /\d /,$temp;
> then i get
> $splitval[0] = 'ARAVIND 25';



I don't get that.

Your installation of perl may be messed up.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

bd 07-05-2003 06:45 PM

Re: dynamic sorting
 
On Sat, 05 Jul 2003 04:11:57 -0700, ARAVIND wrote:

> 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.

--
Freenet distribution not available
According to all the latest reports, there was no truth in any of the
earlier reports.


John W. Krahn 07-05-2003 08:27 PM

Re: dynamic sorting
 
bd wrote:
>
> On Sat, 05 Jul 2003 04:11:57 -0700, ARAVIND wrote:
> >
> > say i have a variable
> > $temp = "ARAVIND 25 BANGALORE";
> > I want just ARAVIND and BANGALORE

>
> 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
--
use Perl;
program
fulfillment

Mina Naguib 07-05-2003 10:14 PM

Re: dynamic sorting
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ARAVIND wrote:
> 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-----


Rohan Romanus Almeida 07-06-2003 11:03 AM

Re: dynamic sorting
 

tadmc@augustmail.com (Tad McClellan) thus wrote:
> ARAVIND <arawind@yahoo.com> wrote:
>
> > 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.

--
arc_of_descent


Tad McClellan 07-06-2003 01:32 PM

Re: dynamic sorting
 
Rohan Romanus Almeida <arc_of_descent@gmx.net> wrote:
> tadmc@augustmail.com (Tad McClellan) thus wrote:



Your followup did not contain anything that I wrote...


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas


All times are GMT. The time now is 01:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.