Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9015187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:41:19+00:00 2026-06-16T03:41:19+00:00

In my perl script Ive filled a two dimensional hash by collecting CDP neighbour

  • 0

In my perl script Ive filled a two dimensional hash by collecting CDP neighbour information from cisco routers in my network via SNMP (in this case ip addresses of the devices). Hashes have allowed me to limit duplicates and capture parent daughter relationships.

$name{$hostIP}{$neighbourIP} = $name;

I’d like to use the hashed data with D3.js (in a dendrogram) to illustrate the router topology or connection relationships and need the data formatted in JSON recursively like:

{
 name: "10.120.5.1",
 children: [
  {
   name: "10.120.5.2",
   children: [
    {
     name: "10.120.5.3",
     children: [
      {
       name: "10.120.5.4"
      },
      {
       name: "10.120.6.1"
      },
      {
       name: "10.120.6.2"
      },
      {
       name: "10.120.6.3"
      }
     ]
    }
   ]
  }
 ]
}

Can someone provide examples using libraries or normal print statements showing how to convert the hash format to JSON similar to the above? Perl is preferred but any language like python, C would help. Also if anyone knows of any open source scripting that does this job already I’d love to compare.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:41:20+00:00Added an answer on June 16, 2026 at 3:41 am

    Does this help with the recursion? Starting from a hash similar to yours, I perform

    • a breadth-first search to see which children can be listed with which parents
    • followed by a depth-first walkthrough of that intermediary structure, to build a hash equivalent to your json sample
    • followed by a straight dump to json.

    Walking the data twice seems sub-optimal, but not doing it results in very deep dendrograms.

    #!/bin/perl
    
    use 5.010; # Enable 'say'. Sorry, old perl
    use strict;
    use warnings;
    use JSON::PP; # Just 'use JSON;' on most systems
    
    # 0. set up some data in adjacency table
    my %name;
    $name{'10.120.5.1'}{'10.120.5.2'}++;
    $name{'10.120.5.2'}{'10.120.5.1'}++;
    
    $name{'10.120.5.2'}{'10.120.5.3'}++;
    $name{'10.120.5.3'}{'10.120.5.2'}++;
    
    $name{'10.120.5.3'}{'10.120.5.4'}++;
    $name{'10.120.5.4'}{'10.120.5.3'}++;
    
    $name{'10.120.5.3'}{'10.120.6.1'}++;
    $name{'10.120.6.1'}{'10.120.5.3'}++;
    
    $name{'10.120.5.3'}{'10.120.6.2'}++;
    $name{'10.120.6.2'}{'10.120.5.3'}++;
    
    $name{'10.120.5.3'}{'10.120.6.3'}++;
    $name{'10.120.6.3'}{'10.120.5.3'}++;
    
    
    # 1. set up helper structures
    # pick a starting point
    (my $root) = keys %name;
    
    # empty structures
    my %nodes = ();
    my %tree  = ();
    my @queue = ($root);
    
    # 2. First pass: BFS to determine child nodes 
    list_children(\%name, \@queue, \%nodes) while @queue;
    
    # 3. Second pass: DFS to set up tree
    my $tree = build_tree($root, \%nodes);
    
    # 4. And use JSON to dump that data structure
    my $json = JSON::PP->new->pretty; # prettify for human consumption
    
    say $json->encode($tree);
    
    sub list_children {
      my $adjac = shift;
      my $queue  = shift;
      my $nodes  = shift;
    
      my $node = shift @$queue;
    
      # all child nodes
      my @children = keys %{$adjac->{$node}};
    
      # except the ones we visited earlier, to avoid loops
      @children = grep { ! exists $nodes->{$_}} @children;
    
      $nodes->{$node} = \@children;
    
      # and toss on the queue
      push @$queue, @children;
    }
    
    sub build_tree {
      my $root  = shift;
      my $nodes = shift;
    
      my @children;
      for my $child (@{$nodes->{$root}}) {
        push @children, build_tree($child, $nodes);
      }
    
      my %h = ('name'     => $root,
               'children' => \@children);
    
      return \%h;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a perl script that loads data from an Excel XLS file into
I'm having perl regex matching problems with a network script I have, I've managed
I've got a Perl script, let's call it A.pl where the first two lines
I've written a perl script that opens two files which contain lists. I want
I am attempting to run php scripts from a perl script on my goDaddy
I've got a Perl script that I want to invoke from a Python script.
I am writing a perl script to parse tab delimited data from standard input.
I'm trying to write a perl script that removes whitespace from XML tags, but
I want to call a perl script from powershell where a parameter is quoted:
I'm trying to create a simple cgi perl script that counts down from 10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.