in reply to Re^2: Help with converting Python script to Perl for CSV sort
in thread Help with converting Python script to Perl for CSV sort

The fact that you're using "my" does not prove that you're using strict, and the use strict; is not there in the code you show. It goes the other way around: if you use strict, then you have to use my (or some other declarator).

If you use the $sheet variable (with a $ sigil), then you declare a scalar variable. If you had used @sheet, you would have declared an array variable. Independently of whether this variable is defined immediately or left "empty" for the moment and populated later.

DATA is a special file handle referring to some data put at the end of your script, after a __DATA__ tag (see examples at the bottom of this post).

If you want to read from a file, then you would have to open the file first and read from the file handle used for opening the file, with something like:

open my $FH, "<", "file.txt" or die "Cannot open file.txt $!"; while (<$FH>) { # ... }

On the $count variable: you initialize it to -1, and you inc rement it in your while loop. The first time through the loop, its value becomes 0, and the line with the header is skipped because a 0 value is evaluated to false in Boolean context.

Line 10 splits the input line stored in $_ and stores the resulting array into the $row array ref. And the next code line stored the row array ref into the $sheet array ref.

Here is how you could minimally change your code to get the sorted result:

use strict; use warnings; use Data::Dumper; my $sheet; my $count = -1; while( <DATA> ) { chomp; $count++; # skip header next unless $count; my $row; @$row = split( /,/, $_ ); push @$sheet, $row; } #my @sorted = sort {$a->[0] <=> $b->[0]} @$sheet; my @sorted = sort {$a->[1] cmp $b->[1]} @$sheet; print Dumper \@sorted; __DATA__ HEADER 1,Beginning C,Beginning C1 2,Beginning C++,Beginning C++1 12,navy blue,navy blue1 3,Python Intro,Python Intro1 8,Baker's dozon,Baker's dozon1 9,Jumbo frames,Jumbo frames1 4,Acme cook book,Acme cook book1 5,Jumping Jack Flash,Jumping Jack Flash1 6,Zebra,Zebra1 7,Ace hardware,Ace hardware1 10,Attack show,Attack show1 11,car 54 where are you,car 54 where are you1 13,navy gold,navy gold1

And this is a slightly improved (simpler) version using arrays instead or array refs. Also using the $. builtin input file line counter, instead of $sount.

use strict; use warnings; use Data::Dumper; my @sheet; while( <DATA> ) { chomp; # skip header next if $. == 1; my @row = split( /,/, $_ ); push @sheet, [@row]; } my @sorted = sort {$a->[0] <=> $b->[0]} @sheet; #my @sorted = sort {$a->[1] cmp $b->[1]} @sheet; print Dumper \@sorted; __DATA__ HEADER 1,Beginning C,Beginning C1 2,Beginning C++,Beginning C++1 12,navy blue,navy blue1 3,Python Intro,Python Intro1 8,Baker's dozon,Baker's dozon1 9,Jumbo frames,Jumbo frames1 4,Acme cook book,Acme cook book1 5,Jumping Jack Flash,Jumping Jack Flash1 6,Zebra,Zebra1 7,Ace hardware,Ace hardware1 10,Attack show,Attack show1 11,car 54 where are you,car 54 where are you1 13,navy gold,navy gold1

I hope this helps.

Replies are listed 'Best First'.
Re^4: Help with converting Python script to Perl for CSV sort
by jasonwolf (Sexton) on Jan 31, 2017 at 18:41 UTC

    This is helping me a great deal; however, I am now confused at another step. When I run the above code you provided - I get the following output.

    __DATA___ E:\code\perl>cli-pl-csvProcess.pl a.cs $VAR1 = [ [ '7', 'Ace hardware', 'Ace hardware1' ], [ '4', 'Acme cook book', 'Acme cook book1' ], [ '10', 'Attack show', 'Attack show1' ], [ '8', 'Baker\'s dozon', 'Baker\'s dozon1' ], [ '1', 'Beginning C', 'Beginning C1' ], [ '2', 'Beginning C++', 'Beginning C++1' ], [ '9', 'Jumbo frames', 'Jumbo frames1' ], [ '5', 'Jumping Jack Flash', 'Jumping Jack Flash1' ], [ '3', 'Python Intro', 'Python Intro1' ], [ '6', 'Zebra', 'Zebra1' ], [ '11', 'car 54 where are you', 'car 54 where are you1' ], [ '12', 'navy blue', 'navy blue1' ], [ '13', 'navy gold', 'navy gold1' ] ];

    How can I format this back into a normal CSV format output? My goal is to create a new file called output like in my hacked python script..

    I can handle the file appending part; however, I do not full understand what is taking place right now. Looks like an Array of an array was printed out, but I am not sure if that is what I want or need.

      You are seeing the output of print Dumper \@sorted; which is a quick way of inspecting the contents of a data structure. Just replace that line with what you had originally.

      #print Dumper \@sorted; open my $fh_out,'>','output.csv' or die "$!"; foreach my $row ( @sorted ) { print $fh_out join( ',', @$row ), "\n"; } close $fh_out;
      poj
Re^4: Help with converting Python script to Perl for CSV sort
by Anonymous Monk on Jan 31, 2017 at 18:36 UTC
    No CPAN modules?
      Sure, why not CSV module. The original question was about sorting, I was just showing how to minimally change the OP code to sort the data.