Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Finding the parent of a text in a file ( is_deeply )

by 1nickt (Canon)
on Sep 04, 2015 at 12:27 UTC ( [id://1140992]=note: print w/replies, xml ) Need Help??


in reply to Finding the parent of a text in a file

You can use is_deeply if you put your data into hashes. I'm making some assumptions here because your post isn't complete (eg that 'entry 1' and 'snmp ok' are keys, not key-value pairs) ... but hopefully this helps.

#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; my ($href1, $href2) = get_data(); is_deeply( $href2, $href1, 'Hashref comparison' ); ## Sub to fetch data below; add code to read files if needed sub get_data { my $href1 = { system => { security => { management_ip => { protocol => { internal => {}, shutdown => {}, allow => {}, entry_1 => { snmp_ok => 'foo', }, exit => {}, }, }, }, }, }; my $href2 = { system => { security => { management_ip => { protocol => { internal => {}, shutdown => {}, allow => {}, entry_1 => { snmp_ok => 'bar', }, exit => {}, }, }, }, }, }; return $href1, $href2; } __END__
Output:
$ perl 1140982.pl 1..1 not ok 1 - Hashref comparison # Failed test 'Hashref comparison' # at 1140982.pl line 8. # Structures begin differing at: # $got->{system}{security}{management_ip}{protocol}{entry_1}{ +snmp_ok} = 'bar' # $expected->{system}{security}{management_ip}{protocol}{entry_1}{ +snmp_ok} = 'foo' # Looks like you failed 1 test of 1. $

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Finding the parent of a text in a file ( is_deeply )
by stevieb (Canon) on Sep 04, 2015 at 13:24 UTC

    Hey 1nickt,

    I recently ran into an issue while updating a module where I needed the functionality of is_deeply(), but I didn't want any test modules in the code, and I didn't want to have to hijack the output with trickery.

    I came across Data::Compare, which simply returns a true/false bool if the structs match. You may be interested to play with this module for this kind of non-test work.

    -stevieb

      Thanks. Looks useful. I like Test::More because it's a core module, so every installation has it, and because it indicates where the data differs (begins to differ). Usually I use this type of comparison where if it fails it should be fatal, so I don't mind the output. I can see how Data::Compare would be a better choice if you wanted to continue anyway. Interestingly, the same author has Test::Differences, which goes in the other direction (is still a Test::, spits TAP to STDOUT, provides a comprehensive diff of the structures).

      The way forward always starts with a minimal test.
Re^2: Finding the parent of a text in a file ( is_deeply )
by ExperimentsWithPerl (Acolyte) on Sep 04, 2015 at 14:40 UTC

    Thanks lnickt for your help.
    This program indeed does work But the issue is I have 2 very huge files to compare and they can have any hierarchy apart from system/security/entry etc (thus I cannot hard code the keys/key-value in the program).
    Also, In this case the difference was in the child of entry 1, similarly in my file the difference can be at the entry 229 (say) , then it would become hard for me to go back till parent and print the same.

      Hi ExperimentsWithPerl,

      The data structures were only hard-coded in my example because it was an example. You would need to create the hashes on the fly from the files, and then compare them.

      If you are wanting to know all the differences in the files, you could use Test::Differences::eq_or_diff_data(), but that still wouldn't print out just the hierarchy as you wish. For that, choroba's solution seems like it may be the way to go.

      But any diff solution (Test::Differences, system diff, whatever) will specify the lines at which the structures differ, so if that's all you need to know it might not be necessary to make the fully qualified "paths" choroba suggested. (It would help if you provided a sample input file and an example of the actual output you'd like to see.)

      The way forward always starts with a minimal test.

        Thanks 1nickt for your inputs.I have got lot to learn.
        You are right...I tried Choroba's solution and it worked nicely.Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1140992]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found