CSV File - Sort data by second field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deppeler
    New Member
    • Jul 2007
    • 40

    CSV File - Sort data by second field

    How do I sort the contents of a flat file based on the second field?
    What I have now sorts by the first field - 0

    [CODE=perl]
    open (BASE, $dbk) || do {&no_open;};
    @sorted = sort(<BASE>);
    foreach $pair (@sorted) {
    @show = split(/,/, $pair);

    close(BASE);
    [/CODE]

    My fields are:
    [0,1,2,3,4,5,6,7 ,8,9,10,11]

    many thanks
    Paul
  • deppeler
    New Member
    • Jul 2007
    • 40

    #2
    I also tried this...

    [CODE=perl]
    open (BASE, $db) || do {&no_open;};
    print BASE "$snumber,$kit, $id,$item_name, $in_out,$itemd_ name,$date_out, $date_in,$name_ out,$staff_out, $staff_in";
    @sorted = <BASE>;

    foreach $line (sort secondfield @sorted) {
    @show = split(/,/,"$line");
    sub secondfield {
    ($snumber,$kit, $id,$item_name, $in_out,$itemd_ name,$date_out, $date_in,$name_ out,$staff_out, $staff_in)= split(/,/,$a);
    ($snumber2,$kit 2,$id2,$item_na me2,$in_out2,$i temd_name2,$dat e_out2,$date_in 2,$name_out2,$s taff_out2,$staf f_in2)= split(/,/,$b);
    return ($kit2 <=> $kit);
    }
    $show[0] etc

    close (BASE);
    }
    [/CODE]

    but it still sorts by the first field.

    thanks

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      You must first parse the contents of your file so that perl can do the sorting on the field. There is no magic available here. You must program it all

      [CODE=perl]
      my @data;
      open(BASE, $db) or die "Can't open $db: $!";
      while (<BASE>) {
      chomp;
      push @data, [split ',', $_];
      }
      [/CODE]

      And then you can sort. The sort documentation itself will tell you how to do this.



      [CODE=perl]
      my @sorted = sort {$a->[1] <=> $b->[1]} @data;
      [/CODE]

      - Miller

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Its so funny, but that is exactly what I was thinking and working on for him, I just got caught up in work coding. Thanks for that confirmation Miller!

        Jeff

        Comment

        • deppeler
          New Member
          • Jul 2007
          • 40

          #5
          Great!...thanks a heap

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by miller
            You must first parse the contents of your file so that perl can do the sorting on the field. There is no magic available here. You must program it all

            [CODE=perl]
            my @data;
            open(BASE, $db) or die "Can't open $db: $!";
            while (<BASE>) {
            chomp;
            push @data, [split ',', $_]
            }
            [/CODE]

            And then you can sort. The sort documentation itself will tell you how to do this.

            http://perldoc.perl.or g/functions/sort.html

            [CODE=perl]
            my @sorted = sort {$a->[1] <=> $b->[1]} @data;
            [/CODE]

            - Miller

            There is a module or two that will handle the backend stuff of sorting mixed data in delimited fields. But really it seems so much easier to just write your own sorting function than to use a module unless a person just can't grasp how the sort() function works.

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              It doesn't surprise me that there would be such modules. Any ones in particular you were thinking of?

              I also considered using Text::CSV_XS just to demonstrate how one would, but it felt like overkill given the data as he expressed it. Must better to just focus the problem at sorting complex data structures.

              - Miller

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by miller
                It doesn't surprise me that there would be such modules. Any ones in particular you were thinking of?


                - Miller
                No, none in particular.

                Comment

                Working...