Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://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 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!.


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

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



  • 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.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found