Prev: FAQ 4.31 How can I split a [character] delimited string except when inside [character]?
Next: benchmarks for perl?
From: Justin C on 18 Feb 2010 11:15 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::Dumper; 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. -- Justin C, by the sea.
From: John Bokma on 18 Feb 2010 12:10 Justin C <justin.0911(a)purestblue.com> writes: > foreach my $item ( sort { $data->{$style}{$a}{1} cmp > $data->{$style}{$b}{1 ^ you want a [ ] there, since sou assign to "item" a ref to an array with 2 elements. And that's what the error you got complained about: you try to treat a ref to an array like it's a ref to a hash. > sub read_in_data { > my %data; > while (<DATA>) { > my ($style, $item, $desc, $qty) = split /:/, $_; > $data{$style}{$item} = [$desc, $qty]; > } > return \%data; > } -- John Bokma j3b Hacking & Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl & Python Development
From: sreservoir on 18 Feb 2010 12:16 On 2/18/2010 11:15 AM, Justin C wrote: > 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... the two messages are equally helpful if you think about it. The second doesn't even explicitly tell you it's a reference problem. If you need to, you can use diagnostics; alternatively, grep perldiag. As for the line references, you can twiddle with your code so that all of the references aren't on a single line. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe"
From: Randal L. Schwartz on 18 Feb 2010 12:07 >>>>> "Justin" == Justin C <justin.0911(a)purestblue.com> writes: This... Justin> foreach my $item ( sort { $data->{$style}{$a}{1} cmp $data->{$style}{$b}{1} } keys %{$data->{$style}} ) { Does not match this... Justin> $data{$style}{$item} = [$desc, $qty]; which is why you're getting an error. I got suspicious when I saw {1}... that's a hash element selection, not an array element. I think you want that to be $data->{$style}{$a}[1] -- note the square brackets, since you have an array there, not a hash. "perldoc perlreftut" might help. And we cover this stuff pretty thoroughly in "Intermediate Perl" (the Alpaca book). print "Just another Perl hacker,"; # the original -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <merlyn(a)stonehenge.com> <URL:http://www.stonehenge.com/merlyn/> Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc. See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
From: Ben Morrow on 18 Feb 2010 16:36
Quoth Justin C <justin.0911(a)purestblue.com>: > 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? You could have answered this easily by running the code from the command-line on your Mac. (You would have got the same error message as from TextMate.) > 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... The change in error message is due to the different perl versions. The 'Can't coerce...' message comes from the pseudohash code, which was removed in 5.10, whereas the 'Not a %s reference' is a generic message for situations where a value holds the wrong kind of ref. Before you start thinking this change was a bad idea, try running perl -le'my $x = [{a => 1}, "b"]; print $x->{a}' on both machines and consider how hard *that* would have been to debug with the 5.8 behaviour :). Ben |