in reply to Sort Problem

Think it through. What exactly are you sorting on? If you want to sort on the first value, then the second, then the third, and sort asciibetically, then just do a sort on @DB. However, if you want to sort then, you have to split each line up into its component parts. Something like the following will help:
my @records = sort { $a->[0] <=> $b->[0] || $a->[1] cmp $b->[1] || $a->[2] cmp $b->[2] || $a->[3] <=> $b->[3] } map { [ split '#', $_ ] } map { chomp; $_ } @DB;

You have to read that code from bottom to top.

  1. You have all the lines in @DB
  2. Take each line and perform a chomp on it. Since chomp() returns the character(s) removed (if any), you need to explicitly return $_
  3. Create an array reference containing the values in the record (using split(), just like you were)
  4. Sort those records based on the elements of the record. <=> is a numeric sort. cmp is an asciibetical sort.

When it's all done, @records will be an array containing array references. If you need more explanation, please ask.

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Replies are listed 'Best First'.
Re: Re: Sort Problem
by Anonymous Monk on Mar 30, 2004 at 16:29 UTC
    I might have a problem because after I splited the values of the array, @DB
    foreach $rec (@DB){ chomp($rec); ($number,$name,$email,$xnumber)=split(/\#/,$rec);

    I am printing the value on the screen, can this sorting be done before?
      Your question indicates you didn't even read what I wrote, nor are you attempting to understand what sorting is in Perl and how it is achieved. Go back and read my reply, Learning Perl by Randal Schwartz, and figure out what it is you're trying to get the computer to do.

      I think the biggest issue you're having is you haven't laid out the steps you want the computer to do. You're trying to design by coding, which is a very poor method. Especially when you're not comfortable with the language or programming in general.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        Thanks for the very intelligent answer, but before you put all this brain power to help another individual, I have one last question for...
        Do you know what this code would do?
        rrty:#$.%34-./;@\{4}Lc(xwy[3,8})/;

        It is kind of complex, since I know you will not have a reasonable answer for that, I am not going to assume that you don't know the language or don't known any computer language in general.
        Thank you!