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
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 | |
by poj (Abbot) on Jan 31, 2017 at 18:56 UTC | |
|
Re^4: Help with converting Python script to Perl for CSV sort
by Anonymous Monk on Jan 31, 2017 at 18:36 UTC | |
by Laurent_R (Canon) on Jan 31, 2017 at 20:04 UTC |