dynamic sorting

Discussion in 'Perl Misc' started by Janek Schleicher, Jul 5, 2003.

  1. 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
     
    Janek Schleicher, Jul 5, 2003
    #1
    1. Advertising

  2. Janek Schleicher

    ARAVIND Guest

    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.
     
    ARAVIND, Jul 5, 2003
    #2
    1. Advertising

  3. ARAVIND <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 5, 2003
    #3
  4. Janek Schleicher

    bd Guest

    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.
     
    bd, Jul 5, 2003
    #4
  5. 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
     
    John W. Krahn, Jul 5, 2003
    #5
  6. Janek Schleicher

    Mina Naguib Guest

    -----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-----
     
    Mina Naguib, Jul 5, 2003
    #6
  7. (Tad McClellan) thus wrote:
    > ARAVIND <> 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
     
    Rohan Romanus Almeida, Jul 6, 2003
    #7
  8. Rohan Romanus Almeida <> wrote:
    > (Tad McClellan) thus wrote:



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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 6, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jordo
    Replies:
    0
    Views:
    454
    Jordo
    Oct 31, 2003
  2. Replies:
    2
    Views:
    1,440
    James Kanze
    Jul 6, 2010
  3. Jason
    Replies:
    0
    Views:
    390
    Jason
    Oct 4, 2006
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    476
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    161
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page