Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: 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 > output.txt 2>&1 output.txt is:
not ok 1 # Failed test ( 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!.

In reply to How to Ask a Datastructures Question (with Test::More) by tphyahoo

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (3)
    As of 2021-03-05 04:59 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (109 votes). Check out past polls.