Question: 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";
}

```

Answer: 
You have `my $row;` which leaves `$row` undefined, and pushing into `@$row` (or `@{$row}`) automatically creates new array due [`autovivification`](http://en.wikipedia.org/wiki/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).
`@$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.