******************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?
Output from perl datastructure.pl > output.txt 2>&1 output.txt is:#!/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 ====
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!.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to Ask a Datastructures Question (with Test::More)
by demerphq (Chancellor) on Oct 06, 2005 at 12:24 UTC |