Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

sorting a list according to numeric values using the function splice()

by changma_ha (Sexton)
on Aug 09, 2010 at 09:51 UTC ( #853764=perlquestion: print w/replies, xml ) Need Help??

changma_ha has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on sorting a list according to numeric values using the function splice()

Replies are listed 'Best First'.
Re: sorting a list according to numeric values using the function splice()
by Corion (Patriarch) on Aug 09, 2010 at 09:56 UTC

    As this question very much looks like homework to me, let me iterate that this is not a script writing service. Learning Perl and programming, whether for homework or for furthering your knowledge works best in my experience if you first have a try at solving the problem yourself. If you have already tried solving the problem yourself, it helps us help you better if you show us what you tried and where you found problems with your approach. That way, we can tailor our answers towards your needs and expand the section where we see your error.

    If you haven't tried anything yourself, start by reading sort and splice. I'm not sure why you would want to use splice at all.

Re: sorting a list according to numeric values using the function splice()
by Ratazong (Monsignor) on Aug 09, 2010 at 11:47 UTC

    What do you want to use splice() for? Do you have a specific algorithm for sorting in your mind?

    You could use the following, trivial (and slow) algorithm that uses splice() to remove single elements from your array.

    • create a new, empty array which will contain the sorted numbers
    • while the unsorted-array contains members
      • loop through the unsorted array to find the smallest element
      • cut the smallest element from the list, using splice
      • push that element at the end of your sorted array

    HTH, Rata (who hopes that you will not use the algorithm above when writing "real" code ;-)

Re: sorting a list according to numeric values using the function splice()
by ikegami (Patriarch) on Aug 09, 2010 at 13:42 UTC
Re: sorting a list according to numeric values using the function splice()
by murugu (Curate) on Aug 09, 2010 at 10:32 UTC
    Are you sure that it is splice? I think it should be sort.

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

      Actually i am a newbie in perl so practicing some of the exercises from a book. That is why i am a bit confuse because i could do sorting easily with sort(), they ask to do with the splice(), Is it possible to do with it?

        So the book is asking you to implement various sorting algorithms in perl, and to not use the built-in sort function. That makes a little more sense, if nothing else than to show why most of the time you will want to use the built-in version of sort.

        --MidLifeXis

Re: sorting a list according to numeric values using the function splice()
by sundialsvc4 (Abbot) on Aug 09, 2010 at 13:24 UTC

    FYI:   As you will see from perldoc -f sort, the sort function allows a comparison-subroutine to be defined.   This might be important in achieving the results that you want, although Perl is already pretty smart at “doing the right thing.”   If you, for example, needed to sort a list of structures, or to perform a sort using multiple keys, this is how you do it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2022-09-28 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (124 votes). Check out past polls.

    Notices?