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!.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.