J
Justin C
As some of you are probably aware I often have difficulty with Perl and
I ask for help here usually about once a month. I try to help myself
where I can, and sometimes, when I'm preparing a minimal example for
posting here, I find the solution to my problem. That's just happened
again, but only due to an error message I received from my text editor
and not Perl directly.
I'm a Mac user, and code using TextMate <URL:http://macromates.com/>, it
has the ability to run code, I don't know how it does it, though, and
it's error message gave me the clue to the solution of my problem. The
error message on my Debian box was non-helpful.
What is likely to be the cause of the non-helpful error message? Is the
version of Perl on my Mac likely more recent - I've just checked and on
my Debian box it's 5.10.0, while on the Mac it's 5.8.8. Does TextMate
have a built in Perl interpreter that gives better error messages?
The error I saw on Debian:
Not a HASH reference at ... line...
The error I saw from TextMate:
Can't coerce array into hash at ... line...
I prepared a minimal code example, so here's the broken code, I'm sure
most of you will know what the problem is at a glance, unfortunately it
wasn't obvious to me, and as there are 3 references to hashes on that line
I didn't know where to start. I erroneously spent quite a while looking
at the part of the line that was fine, just because there was only one
error, and not two - obviously the program halted before checking
further, as it would with an error as opposed to a warning.
e strict;
use warnings;
use Data:umper;
my $data = read_in_data();
# print Dumper $data;
foreach my $style ( sort keys %{$data}) {
foreach my $item ( sort { $data->{$style}{$a}{1} cmp $data->{$style}{$b}{1} } keys %{$data->{$style}} ) {
printf("%-20s %-40s %5d\n", $item, @{$data->{$style}{$item}}[0], @{$data->{$style}{$item}}[1]);
}
}
sub read_in_data {
my %data;
while (<DATA>) {
my ($style, $item, $desc, $qty) = split /:/, $_;
$data{$style}{$item} = [$desc, $qty];
}
return \%data;
}
__DATA__
TS:TS/ZEP/HERMIT/D:LED ZEPPELIN hermit TSXL:6
TS:TS/MET/MASTERO/D:METALLICA master of puppets TSXL:5
TS:TS/AC/CLASSIC/B:AC/DC classic red logo TS M:12
TS:TS/MET/MASTERO/C:METALLICA master of puppets TS L:7
BB:BB/MET/MASTERO:METALLICA master of puppets BB Cap:17
BB:BB/AC/LOGO:AC/DC logo BB Cap:10
Justin.
I ask for help here usually about once a month. I try to help myself
where I can, and sometimes, when I'm preparing a minimal example for
posting here, I find the solution to my problem. That's just happened
again, but only due to an error message I received from my text editor
and not Perl directly.
I'm a Mac user, and code using TextMate <URL:http://macromates.com/>, it
has the ability to run code, I don't know how it does it, though, and
it's error message gave me the clue to the solution of my problem. The
error message on my Debian box was non-helpful.
What is likely to be the cause of the non-helpful error message? Is the
version of Perl on my Mac likely more recent - I've just checked and on
my Debian box it's 5.10.0, while on the Mac it's 5.8.8. Does TextMate
have a built in Perl interpreter that gives better error messages?
The error I saw on Debian:
Not a HASH reference at ... line...
The error I saw from TextMate:
Can't coerce array into hash at ... line...
I prepared a minimal code example, so here's the broken code, I'm sure
most of you will know what the problem is at a glance, unfortunately it
wasn't obvious to me, and as there are 3 references to hashes on that line
I didn't know where to start. I erroneously spent quite a while looking
at the part of the line that was fine, just because there was only one
error, and not two - obviously the program halted before checking
further, as it would with an error as opposed to a warning.
e strict;
use warnings;
use Data:umper;
my $data = read_in_data();
# print Dumper $data;
foreach my $style ( sort keys %{$data}) {
foreach my $item ( sort { $data->{$style}{$a}{1} cmp $data->{$style}{$b}{1} } keys %{$data->{$style}} ) {
printf("%-20s %-40s %5d\n", $item, @{$data->{$style}{$item}}[0], @{$data->{$style}{$item}}[1]);
}
}
sub read_in_data {
my %data;
while (<DATA>) {
my ($style, $item, $desc, $qty) = split /:/, $_;
$data{$style}{$item} = [$desc, $qty];
}
return \%data;
}
__DATA__
TS:TS/ZEP/HERMIT/D:LED ZEPPELIN hermit TSXL:6
TS:TS/MET/MASTERO/D:METALLICA master of puppets TSXL:5
TS:TS/AC/CLASSIC/B:AC/DC classic red logo TS M:12
TS:TS/MET/MASTERO/C:METALLICA master of puppets TS L:7
BB:BB/MET/MASTERO:METALLICA master of puppets BB Cap:17
BB:BB/AC/LOGO:AC/DC logo BB Cap:10
Justin.