Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to Ask a Datastructures Question (with Test::More)

by tphyahoo (Vicar)
on Oct 06, 2005 at 10:00 UTC ( #497872=perlmeditation: print w/ replies, xml ) Need Help??

This is the fourth post in the How to Ask a Question (With Test::More) series, and assumes you have read the first, main post. Since you are asking a data structures question, you have hopefully already read perllol and perldsc before coming here, like our hypothetical question asker. Even if you haven't, though, you will probably get helped anyway.... and if you base your post on this handy template and you may even get helped with the actual problem you are having, rather than some other problem ;)

******************BEGIN QUESTION TEMPLATE************************

Masterful Monks, I am tripping over this complicated datastructure. I have read perllol annd perldsc, but my brain hurts, and I pray one of your multitude will take a few minutes out of their day to take my pain away. My test just keeps failing no matter what I do. When I dump the data, there seem to be two extra elements in my SET2 hashref. How are they getting there? What the heck is causing this to fail?

#!/usr/bin/perl -w use strict; use warnings; use Test::More qw(no_plan); use Data::Dumper; my $expected = { 'SET1' => [ [ '0','100','BOOK'], [ '1','150','PENCIL'], ], 'SET2' => [ [ '2','111','ERASER'], [ '2','200','PEN'], [ '0','220','BLACKBOARD'], [ '1','300','CHALK'], ] }; my $key; my %hoa; my @arr; while ( <DATA> ) { chomp; next if (/^\s+/); if ( /^SET/ ) { my @KEY = split (/[\s:]/,$_); $key = $KEY[2]; } elsif (/^\d/) { my @INST = split(/,/,$_); push @arr, [@INST]; } $hoa{$key} = [ @arr]; } Test::More::is_deeply (\%hoa, $expected); print '%hoa' . "\n" . Dumper(\%hoa); __DATA__ SET: SET1 0,100,BOOK 1,150,PENCIL ==== SET: SET2 2,111,ERASER 2,200,PEN 0,220,BLACKBOARD 1,300,CHALK ====
Output from perl datastructure.pl > output.txt 2>&1 output.txt is:
not ok 1 # Failed test (datastructures.pl at line 37) # Structures begin differing at: # $got->{SET2}[0][0] = '0' # $expected->{SET2}[0][0] = '2' %hoa $VAR1 = { 'SET1' => [ [ '0', '100', 'BOOK' ], [ '1', '150', 'PENCIL' ] ], 'SET2' => [ $VAR1->{'SET1'}[0], $VAR1->{'SET1'}[1], [ '2', '111', 'ERASER' ], [ '2', '200', 'PEN' ], [ '0', '220', 'BLACKBOARD' ], [ '1', '300', 'CHALK' ] ] }; 1..1 # Looks like you failed 1 tests of 1.

What the heck am I doing wrong?

******************END QUESTION TEMPLATE************************

This post was inspired by, and reuses code from Re: Fail to update an array in HoAoA and Does Data::Dumper give you eye strain? Use Test::More::is_deeply() for debugging!.

Comment on How to Ask a Datastructures Question (with Test::More)
Select or Download Code
Re: How to Ask a Datastructures Question (with Test::More)
by demerphq (Chancellor) on Oct 06, 2005 at 12:24 UTC

    Yeah, well, personally I'm not a huge fan of Test::More::is_deeply(). And I think I said it in that thread too. Anyway....

    A quick look at the two data structures using a proper dumper reveals the problem, as does a cursory review of the code. Your problem is that the code doesnt change the array being pushed into when a new key is found. The following is more or less how I would implement it.

    #!/usr/bin/perl -w use strict; use warnings; use Test::More qw(no_plan); use Data::Dump::Streamer; my $expected = { 'SET1' => [ [ '0','100','BOOK'], [ '1','150','PENCIL'], ], 'SET2' => [ [ '2','111','ERASER'], [ '2','200','PEN'], [ '0','220','BLACKBOARD'], [ '1','300','CHALK'], ] }; my %hoa; my $key; while ( <DATA> ) { chomp; next unless /^\S/; if ( /^SET/ ) { (undef,$key) = split (/[\s:]+/,$_,2); } elsif (/^\d/) { my @rec = split(/,/,$_); push @{$hoa{$key}}, \@rec; } } print Dump(\%hoa,$expected)->Names('got','expect')->Out(); Test::More::is_deeply (\%hoa, $expected); __DATA__ SET: SET1 0,100,BOOK 1,150,PENCIL ==== SET: SET2 2,111,ERASER 2,200,PEN 0,220,BLACKBOARD 1,300,CHALK ====
    ---
    $world=~s/war/peace/g

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://497872]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-08-28 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (257 votes), past polls