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

how to sort string in perl

by Priti24 (Novice)
on Dec 05, 2012 at 11:22 UTC ( #1007268=perlquestion: print w/ replies, xml ) Need Help??
Priti24 has asked for the wisdom of the Perl Monks concerning the following question:

suppose i have one file having approx 1000 names in it. i want to sort that name without using function sort. and also i have to print sorted names in to another file

plz help me out asap....

thnks in advance

Comment on how to sort string in perl
Re: how to sort string in perl
by marto (Bishop) on Dec 05, 2012 at 11:26 UTC
Re: how to sort string in perl
by rovf (Priest) on Dec 05, 2012 at 11:30 UTC
    In addition to what marto wisely said:

    Just writing plz help me is a bit unspecific. You need to explain more in detail, at which point you got stuck.

    -- 
    Ronald Fischer <ynnor@mm.st>

      thnks 4 help.... i need this on urgent basis thats why i wrote this... if u can't reply then there is no need of your comments.

        i need this on urgent basis thats why i wrote this... if u can't reply then there is no need of your comments.
        If you want a reply, you need to define what's your problem. I assume you are not looking for somebody to write the whole program for you, so presumably you have come to some point, and don't know how to continue from there. But without knowing at which point you are stuck, how could anybody possibly help you?


        -- 
        Ronald Fischer <ynnor@mm.st>

        thnks 4 help.... i need this on urgent basis thats why i wrote this... if u can't reply then there is no need of your comments

        Where are you interviewing?

            if u can't reply then there is no need of your comments.

        Snarky comments like that are no way to engender in the Monks here any sort of desire to help you. I, for one, am offended by your attitude. This is a peer reviewed "forum", for a lack of better word, where the practice of good Perl coding is furthered. It is not your personal help desk.

        If you had attempted to solve the problem yourself and posted your code explaining what went wrong you'd get more help than by just saying "I want to write code to predict the stock market out to the year 2115, where do I start?"

        For further guidance I'd suggest you read the tutorials at Understanding and Using PerlMoks especially this article. If you follow those guidelines you'll find the Monastery a much friendlier place.


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: how to sort string in perl
by roboticus (Canon) on Dec 05, 2012 at 11:41 UTC

    Priti24:

    This should provide you a couple hints:

    $ cat bozosort.pl #!/usr/bin/perl use strict; use warnings; my @list = (1, 2, 3, 9, 6, 4, 5, 7, 0); while (@list) { my $idx = int rand @list; next if grep { $list[$idx] > $_ } @list; say splice @list, $idx, 1; }

    On my machine, this produces:

    $ perl bozosort.pl 0 1 2 3 4 5 6 7 9

    You'll want to make appropriate changes to improve performance, use files, etc.

    Update: Hung a lantern on it.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I like stooge sort more. It can be much more efficient: I used functional-programming techniques which means it can be trivially parallelised over many CPUs.

      sub stoogesort { my (@lst) = @_; if ($lst[-1] < $lst[0]) { # swap @lst[0, -1] = @lst[-1, 0]; } return @lst if (@lst < 3); # thirds my $zero = 0; my $one = int(@lst / 3); my $two = int($#lst * 2 / 3); my $three = $#lst; for my $range ( [ $zero .. $two ], [ $one .. $three ], [ $zero .. $two ] ) { @lst[ @$range ] = stoogesort( @lst[ @$range ] ); } return @lst; } my @list = (1, 2, 3, 9, 6, 4, 5, 7, 0); print join(" ", stoogesort(@list)), "\n";
Re: how to sort string in perl
by Arunbear (Parson) on Dec 05, 2012 at 11:58 UTC
Re: how to sort string in perl
by karlgoethebier (Priest) on Dec 05, 2012 at 13:26 UTC
Re: how to sort string in perl
by Anonymous Monk on Dec 05, 2012 at 13:31 UTC
    $ sort <infile >outfile
Reaped: Re: how to sort string in perl
by NodeReaper (Curate) on Dec 05, 2012 at 13:31 UTC
Re: how to sort string in perl
by davido (Archbishop) on Dec 05, 2012 at 16:48 UTC

    You're in luck. If someone were asking me to sort strings, sort would be my second choice too. My first choice would probably be Unicode::Collate. Wrap your solution around that module, and you've satisfied the requirement of not using sort, while at the same time providing a better solution that sort would have offered in the first place.

    I'm aware that this answer probably is of little help to you in your interview; the interviewer probably wants to see you write a sort. But if he's interested in getting the job done well, maybe he will respect the answer. ;)


    Dave

Re: how to sort string in perl
by Plankton (Priest) on Dec 28, 2012 at 00:13 UTC
    not sorting them is sort of a sort

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1007268]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-12-22 06:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (111 votes), past polls