0

I had to write a csv sorting code for which I used the following code :

foreach(@files){
    if(/\.csv$/i) { # if the filename has .csv at the end
        push(@csvfiles,$_);
    }
}

foreach(@csvfiles) {
    $csvfile=$_;
    open(hanr, "D:\\stock\\".$csvfile)or die"error $!\n"; # read handler
    open(hanw , ">D:\\stock\\sorted".$csvfile) or die"error $! \n"; # write handler for creating new sorted files
    @lines=();
    @lines=<hanr>;

    foreach $line (@lines){
        chomp $line;
        $count++;
        next unless $count; # skip header i.e the first line containing stock details
        my $row;
        @$row = split(/,/, $line );
        push @$sheet2 , $row;
    }

    foreach my $row (
      sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @$sheet2
    ) # sorting based on date ,then stockcode
    {
        chomp $row;
        print hanw join (',', @$row ),"\n";
    }
    @$sheet2 = ();
    $count   = -1;
    close(hanw);
    close(hanr);
}

However I do not understand what @$row is ..also I understand sorting a normal array @sheet2 comparing column 0 and 1 ..but if someone would explain the whole thing it would be wonderful:

    @$row = split(/,/, $line );
    push @$sheet2 , $row;
}

foreach my $row ( sort {$a->[0] cmp $b->[0] || $a->[1] cmp $b->[1]}  @$sheet2 )
{
    *print hanw join (',', @$row ),"\n";
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
avinashkr
  • 512
  • 1
  • 5
  • 20
  • Welcome to SO and perl. Please include [`use strict;`](http://perldoc.perl.org/strict.html) and [`use warnings;`](http://perldoc.perl.org/warnings.html) at the top of EVERY perl script. It will make you [a better programmer](http://stackoverflow.com/questions/8023959/why-use-strict-and-warnings), and save you an incalculable amount of time chasing down obvious bugs. – Miller Jul 04 '14 at 06:09
  • @miller i am using it .this was actually a subroutine in a M\threaded csv sorter i was writing i have used strict,warnings;since i saw so many point it out. – avinashkr Jul 04 '14 at 06:55

2 Answers2

3

You have my $row; which leaves $row undefined, and pushing into @$row (or @{$row}) automatically creates new array due autovivification perl feature.

In case of

sort {
  $a->[0] cmp $b->[0] || 
  $a->[1] cmp $b->[1]
}
@$sheet2

@$sheet2 is array of array structure, and @$sheet2 arrays are sorted by first and second element from sub-array (string sort due cmp operator; if $a->[0] and $b->[0] are equal then $a->[1] and $b->[1] are compared).

mpapec
  • 50,217
  • 8
  • 67
  • 127
  • i am sorry i am a java programmer ,just started learning perl ..so its a array of array and represented as @[$varname].i am still not clear about state of @$row and what is being pushed into @$sheet2 since $row is undefined – avinashkr Jul 03 '14 at 07:10
  • 1
    @avinashkr check Data::Dumper core module, ie. `use Data::Dumper; print Dumper $sheet2`. `$row` is not undefined _after_ autovivification and then it holds array reference. – mpapec Jul 03 '14 at 07:32
  • ok i used data::dumper module and if printing it to a file it prints in the var1=[...],var=[...].so again how could i format it to csv format – avinashkr Jul 04 '14 at 07:00
1

@$row dereferences an array reference.

sort {$a->[0] cmp $b->[0] || $a->[1] cmp $b->[1]} This will compare the first entry in both arrays and if these are equals the second entry will be compared.

Hope this helps.

Zaid
  • 36,680
  • 16
  • 86
  • 155
Jens
  • 67,715
  • 15
  • 98
  • 113
  • @$row = split(/,/, $line );//for each line ot will split and give n number of coulumns and then next line push @sheet2 ,$ row; // $row is pushed ;what exactly is happening – avinashkr Jul 03 '14 at 06:44