Variable length array of arrays into tree structure - help 
Author Message
 Variable length array of arrays into tree structure - help

I need help figuring out how to print this properly.

I have generated an array of arrays from a csv file.  Here is an example
of the data stored in the array (note:  the ' - ' are just element
dividers and not actual elements.  They are just text).

ADMSND70AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.34
ARTHND16AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.26
CAVWND48AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.6
CRYSND04AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.46
CVLRND10AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.90
DYTNND01AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.50
co-gateway - Sessions - NOC-CO - Servers - 10.70.64.33
Genview-EMS - Sessions - NOC-CO - Servers - 172.16.19.254
Mayberry (external) - Sessions - NOC-CO - Servers - 1.3.5.4
Mayberry (internall) - Sessions - NOC-INT - Servers - 172.16.18.103
OPIE2 (SUN NTP SERVER) - Sessions - NOC-INT - Servers - 172.16.18.102
10008PKRV_L - Sessions - NOC-INT - Core - Rotuers - Console -

10008PKRV_R - Sessions - NOC-INT - Core - Rotuers - Console -

What I need to do is setup printing of this data in the following tree
format:

Sessions
 >NOC-CO
    >AFC
      >ADMSND70AFC.01
        >172.16.22.34
      >ARTHND16AFC.01
        >172.16.22.26
      >CAVWND48AFC.01
        >172.16.22.6
      >CRYSND04AFC.01
        >172.16.22.46
      >CVLRND10AFC.01
        >172.16.22.90
      >DYTNND01AFC.01
        >172.16.22.50
    >Servers
      >co-gateway
        >10.70.64.33
      >Genview-EMS
        >172.16.19.254
      >Mayberry (external)
        >1.3.5.4
  >NOC-INT
    >Servers
      >Mayberry (internall)
        >172.16.18.103
      >OPIE2 (SUN NTP SERVER)
        >172.16.18.102
    >Core
      >Routers
        >Console
          >PKRV_L
            >10.10.17.68
          >PKRV_R
            >10.10.17.68

I.E.
- '172.16.22.34' is a child of 'ADMSND70AFC.01', which is a child of
'AFC', which is a child of 'NOC-CO', which is a child of 'Sessions'.
- '10.10.17.68' is a child of 'PKRV_R', which is a child of 'Console',
which is a child of 'Routers', which is a child of 'Core', which is a
child of 'NOC-INT', which is a child of 'Session'.

I am going to place this in a XML for bookmarks for KDE Konsole.  I need
to create a tree/folder structure.

The array inside the array ('row' - Sorry for calling it that, but it
fits) can be variable length.

I've been looking for some form of 'tree' function or script with no luck.

Anyone have any advice?



Sat, 24 Aug 2013 17:31:02 GMT  
 Variable length array of arrays into tree structure - help

Quote:

> I need help figuring out how to print this properly.

> I have generated an array of arrays from a csv file.

Hopefully you're using one of the CSV modules.

You probably want to use hashes too.

Quote:
> Here is an example
> of the data stored in the array (note:  the ' - ' are just element
> dividers and not actual elements.  They are just text).

To show/view the actual data structure, try Data::Dumper. e.g.

use Data::Dumper;
print Dumper( $yourdata );

That way you/we can clearly see the structure of your data.

Quote:

> ADMSND70AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.34
> ARTHND16AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.26
[...]

> What I need to do is setup printing of this data in the following tree
> format:

> Sessions
>  >NOC-CO
>     >AFC
>       >ADMSND70AFC.01
>         >172.16.22.34
[...]
> I.E.
> - '172.16.22.34' is a child of 'ADMSND70AFC.01', which is a child of
> 'AFC', which is a child of 'NOC-CO', which is a child of 'Sessions'.
> - '10.10.17.68' is a child of 'PKRV_R', which is a child of 'Console',
> which is a child of 'Routers', which is a child of 'Core', which is a
> child of 'NOC-INT', which is a child of 'Session'.


Quote:
}, '172.16.22.34' );

for my $xyz ( sort keys %data})
{
   print "$xyz\n";
   for my $session ( sort keys %{ $data{ $xyz } })
   {
      print " >$session\n";
      for my $aaa ( sort keys %{ $data{ 'Sessions' }{ $session } } )
      {
         print "   >$aaa\n";
         etc....
      }
   }

Quote:
}

but.. there's probably no reason to actually write that, except for
debugging, since your goal is to create XML.  Using Dumper will be
better, but a bit more verbose.

Quote:

> I am going to place this in a XML for bookmarks for KDE Konsole.  I need
> to create a tree/folder structure.

> The array inside the array ('row' - Sorry for calling it that, but it
> fits) can be variable length.

> I've been looking for some form of 'tree' function or script with no luck.

A 'tree' is nothing more than a data structure:

perldoc perldsc

To produce the XML, XML::Simple has an XMLout method that might be helpful.

perldoc XML::Simple

Create the correct data structure, and use a couple of options to
XMLout, and it could produce what's needed in very few lines of code.
You could start with a similar XML file, use XMLin and Dumper, to
see what data structure you should use as a model.

Also, since you're starting with a CSV file, XML::CSV might be a good
option:

http://search.cpan.org/~isterin/XML-CSV-0.15/CSV.pm



Sat, 24 Aug 2013 18:36:07 GMT  
 Variable length array of arrays into tree structure - help

Quote:

> }, '172.16.22.34' );

The problem I am having is that I'm dealing with variable lengths.  
Pushing into the above array is 5 elements.  There are other elements
that are 3 elements long, 4 elements long, etc.  So it's variable.


Sat, 24 Aug 2013 19:43:40 GMT  
 Variable length array of arrays into tree structure - help

Quote:


>> }, '172.16.22.34' );

> The problem I am having is that I'm dealing with variable lengths.  
> Pushing into the above array is 5 elements.  There are other elements
> that are 3 elements long, 4 elements long, etc.  So it's variable.

What happened when you tried it?

my %data;


%data is a hash, and 'Sessions', 'BLAH', etc. are referred to as keys.

I strongly suggest spending at least a day reading/experimenting with
the code in: perldoc perldsc

Once you understand the various basic data structures it'll be
much easier to do what you want.



Sat, 24 Aug 2013 20:44:37 GMT  
 Variable length array of arrays into tree structure - help

Quote:


>> }, '172.16.22.34' );

> The problem I am having is that I'm dealing with variable lengths.
> Pushing into the above array is 5 elements.  There are other elements
> that are 3 elements long, 4 elements long, etc.  So it's variable.

So instead of a multi-key one-hit, simply iterate, a level at a time.

  BugBear



Sun, 25 Aug 2013 10:27:28 GMT  
 Variable length array of arrays into tree structure - help

Quote:
> I have generated an array of arrays from a csv file. ?Here is an example
> of the data stored in the array (note: ?the ' - ' are just element
> dividers and not actual elements. ?They are just text).

> ADMSND70AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.34
> ARTHND16AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.26
> CAVWND48AFC.01 - Sessions - NOC-CO - AFC - 172.16.22.6

In general, using a hash for this kind of task is a lot easier. When
you iterate through your file, destructure each line and stuff the
data elements into a hash, like this:

my %sessions;
while (<DATA>)
{
  next unless /\w/;
  chomp;
  my ($key, $ses, $noc, $afc, $ip) = split(/ - /, $_);
  $sessions{$noc}{$afc}{$ip} = $ip if $ses =~ /sessions/i;

Quote:
}

Of course, you can have multiple hashes, and sort your data however
you like. Once you have your data in memory, it's trivial to print
your hash, like this:

foreach my $k1 (keys %sessions)
{
  print "$k1 =>\n";
  foreach my $k2 (keys %{$sessions{$k1}})
  {
    print "    $k2 =>\n"
    foreach my $k3 (keys %{$sessions{$k1}{$k2}})
    {
      print "        $k3 => $sessions{$k1}{$k2}{$k3}\n";
    }
  }

Quote:
}

You can also use the increment operator (++) to accumulate the number
of times each element appears, which is sometimes useful. Often, I
will store the entire line as a string as the value of the bottom hash
element, which makes it easy to get at the data after it's been sliced
and diced in various ways.

I haven't tested the above code, it probably contains errors, but you
get the idea.

CC.



Sun, 25 Aug 2013 15:22:44 GMT  
 Variable length array of arrays into tree structure - help
Thanks to everyone for replying.  Please understand that I've never
really had formal perl scripting training, and that I'm trying to pick
apart pieces I'm finding online.  I understand a lot of the concepts, but
not the practical knowledge.  No, this is not a homework assignment!  heh

What I am having a hard time grasping is that there are sometimes going
to be 5 elements in the array, sometimes more, sometimes less.  This will
have to be incorporated into the loops.

Also, here is my oversimplified simplistic view of the XML I'm trying to
create understanding that it's not a true XML.
<Session>
  <NOC-CO>
    <AFC>
      <ADMSND70AFC.01>
        <172.16.22.34>
      </ADMSND70AFC.01>
      <CAVWND48AFC.01>
        <172.16.22.6>
      </CAVWND48AFC.01>
    </AFC>
    <Servers>
      <co-gateway>
        <10.70.64.33>
      </co-gateway>
    </Servers>
  </NOC-CO>
  <NOC-INT>
    <Servers>
      <junk>
        <10.70.64.33>
      </junk>
    </Servers>
    <Routers>
      <Console>
        <PKRV_L>
          <10.10.17.68>
        </PKRV_L>
    </Routers>
  </NOC-INT>
</Session>



Mon, 26 Aug 2013 18:50:09 GMT  
 Variable length array of arrays into tree structure - help


Wed, 18 Jun 1902 01:00:00 GMT  
 Variable length array of arrays into tree structure - help
I need to produce a 'tree'

  Sessions
     /  \
    /    \
 NOC-CO  NOC-INT
 /  \     /  \



Mon, 26 Aug 2013 20:12:22 GMT  
 Variable length array of arrays into tree structure - help

  W> I need to produce a 'tree'
  W>   Sessions
  W>      /  \
  W>     /    \
  W>  NOC-CO  NOC-INT
  W>  /  \     /  \

looks like you did a fine job there. and your perl question is?

uri

--

-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------



Mon, 26 Aug 2013 20:52:12 GMT  
 Variable length array of arrays into tree structure - help

Quote:
> looks like you did a fine job there. and your perl question is?

> uri

This is why I don't like asking innocent questions and try to learn.  
There is always someone in the bunch...

I have a perl array with the following data seperated by colons. The data
is stored in an array of arrays. The length of the array inside the array
(forgive me-'Row'Unsure ) is variable length.

        Code    

Sessions:NOC-CO:Servers:andy:(Netra t1 105 DNS server):192.168.18.104
Sessions:NOC-CO:Servers:Blackserver:192.168.19.117
Sessions:NOC-CO:Servers:Blackserver:VNC:tunnel:192.168.19.117
Sessions:NOC-CO:Servers:Blueserver:192.168.19.115
Sessions:NOC-CO:Servers:co-gateway:10.70.64.33
Sessions:NOC-CO:Servers:Genview-EMS:192.168.19.254
Sessions:NOC-CO:Servers:Mayberry:(external):192.168.75.8
Sessions:NOC-CO:Servers:Mayberry:(internall):192.168.18.103
Sessions:NOC-CO:Servers:OPIE2:192.168.18.102
Sessions:NOC-INT:Core:Rotuers:Console:10008PKRV_L:192.168.17.68
Sessions:NOC-INT:Core:Rotuers:Console:10008PKRV_R:192.168.17.68

I need to print a tree format so I can get the data into an XML where
'Sessions' is the root, NOC-CO and NOC-INT are a branch/children, then
'Servers' is a branch of NOC-CO and 'Core' is a child of NOC-INT, etc.

So it presents itself like this:
        Code    

                          Sessions
                          /      \
                         /        \
                     NOC-CO     NOC-INT

etc.

But it has to be in XML format:

        Code    

<Sessions>
  <NOC-CO>
    <stuff>
       some foo xml stuff....
    </stuff>
  </NOC-CO>
  <NOC-INT>
    <otherstuff>
       some other foo xml stuff....
    </otherstuff>
  </NOC-INT>
</Sessions>



Mon, 26 Aug 2013 21:02:49 GMT  
 Variable length array of arrays into tree structure - help

Quote:
> I need to produce a 'tree'

> ? Sessions
> ? ? ?/ ?\
> ? ? / ? ?\
> ?NOC-CO ?NOC-INT
> ?/ ?\ ? ? / ?\

What do you think a nested hash is? The nice thing about using hashes
is that you can get (and write) the values using whatever you want to
use for the XML elements. There's not any real limit to the depth (or
width) of your hash other than memory, and you can sort them any way
you want.

CPAN probably has modules that can parse and emit XML, but it may be
just as easy to write your own. If you need help, 'Learning Perl
Objects, References, and Modules' can be had from Amazon used for
$3.50 plus shipping, and it get you where you want to be. 'Data
Munging with Perl' can be had for $3.95 from the same source, and it
explicitly covers XML. I can recommend both of these books -- they
overlap but deal with the subject matter from different points of
view.

CC.



Mon, 26 Aug 2013 21:04:56 GMT  
 Variable length array of arrays into tree structure - help
Thanks for the advice.  I will educate myself on nested hashes and see
what I can come up with.


Mon, 26 Aug 2013 21:08:58 GMT  
 Variable length array of arrays into tree structure - help

  >> looks like you did a fine job there. and your perl question is?
  >>
  >> uri

  W> This is why I don't like asking innocent questions and try to learn.  
  W> There is always someone in the bunch...

pot, meet kettle. how in hell would you expect someone to answer a post
like that? you didn't even ask a question. you didn't say ANYTHING about
input or output. you are the one to blame, i just called you out.

  W> I have a perl array with the following data seperated by colons. The data
  W> is stored in an array of arrays. The length of the array inside the array
  W> (forgive me-'Row'Unsure ) is variable length.

  W>         Code    

  W> Sessions:NOC-CO:Servers:andy:(Netra t1 105 DNS server):192.168.18.104
  W> Sessions:NOC-CO:Servers:Blackserver:192.168.19.117
  W> Sessions:NOC-CO:Servers:Blackserver:VNC:tunnel:192.168.19.117
  W> Sessions:NOC-CO:Servers:Blueserver:192.168.19.115
  W> Sessions:NOC-CO:Servers:co-gateway:10.70.64.33
  W> Sessions:NOC-CO:Servers:Genview-EMS:192.168.19.254
  W> Sessions:NOC-CO:Servers:Mayberry:(external):192.168.75.8
  W> Sessions:NOC-CO:Servers:Mayberry:(internall):192.168.18.103
  W> Sessions:NOC-CO:Servers:OPIE2:192.168.18.102
  W> Sessions:NOC-INT:Core:Rotuers:Console:10008PKRV_L:192.168.17.68
  W> Sessions:NOC-INT:Core:Rotuers:Console:10008PKRV_R:192.168.17.68

that isn't an array of arrays - that is just a list of lines. SHOW YOUR
PERL CODE. SHOW REAL DATA. use Data::Dumper if you have real data.

  W> I need to print a tree format so I can get the data into an XML where
  W> 'Sessions' is the root, NOC-CO and NOC-INT are a branch/children, then
  W> 'Servers' is a branch of NOC-CO and 'Core' is a child of NOC-INT, etc.

printing a tree will not get you into xml.

  W> So it presents itself like this:
  W>         Code    

  W>                           Sessions
  W>                           /      \
  W>                          /        \
  W>                      NOC-CO     NOC-INT

  W> etc.

  W> But it has to be in XML format:

perl doesn't do xml itself. xml is a text form of a tree. perl stores
trees with references. you use a module to convert a perl tree to xml.

you need to learn how to express your problems better before you can get
help. also this is about helping with perl, not writing your
programs. what perl have you attempted for this?

uri

--

-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------



Mon, 26 Aug 2013 21:32:51 GMT  
 Variable length array of arrays into tree structure - help

Quote:
> pot, meet kettle. how in hell would you expect someone to answer a post
> like that? you didn't even ask a question. you didn't say ANYTHING about
> input or output. you are the one to blame, i just called you out.

> perl doesn't do xml itself. xml is a text form of a tree. perl stores
> trees with references. you use a module to convert a perl tree to xml.

> you need to learn how to express your problems better before you can get
> help. also this is about helping with perl, not writing your programs.
> what perl have you attempted for this?

> uri

Other's have at least simply pointed me into a direction, which is all
I'm after.  As by the ORIGINAL post I made:

Quote:
> Anyone have any advice?

instead of asking 'Can anyone write this for me?'

Per your request, here is the perl script I have so far.  It's not
written by a 'Professional' perl programmer, just someone trying to learn
by picking up bits and pieces:

#!/usr/bin/perl
use XML::Simple;

my $infile = '/media/Docs/Scripts/Perl/Putty/TEST.csv';

open (CSVFILE, $infile) || die ("Could not open $infile!");

while ($line = <CSVFILE>) {
  $line =~ s/\"//g;
  $line =~ s/\r|\n//g;
  $line =~ s/\\/:/g;
  ($C1,$C2,$C3,$C4) = split ',', $line;


Quote:
}

close (CSVFILE);

for ($i = 0; $i <= $stuffcount-1; $i++) {
  $text = $stuff[$i][0];


Quote:
}

for ($h = 1; $h < $stuffcount; $h++) {
  for (my $i = 0; $i < $#{$c2array[$h]}+1; $i++) {

  }




Quote:
}

for $i ( 0 .. $#final ) {

Quote:
}


[ Sessions NOC-CO Pannaway PKRV NETWORK PKRV PKRV.42.04 CO 07
10.20.23.233 ]
[ Sessions NOC-CO Pannaway PKRV NETWORK PKRV PKRV.42.05 CO 08
10.20.23.231 ]
[ Sessions NOC-CO Pannaway PKRV NETWORK PKRV PKRV.42.06 CO 09
10.20.23.229 ]
[ Sessions NOC-CO Pannaway PKRV NETWORK PKRV PKRV.43.01 CO 10
10.20.23.227 ]
[ Sessions NOC-CO Pannaway PKRV NETWORK PKRV PKRV.43.02 CO 11
10.20.23.225 ]
[ Sessions NOC-CO Pannaway PKRV NETWORK PSEK PSEK.41.01 10.20.25.252 ]
[ Sessions NOC-CO Pannaway PKRV NETWORK PSEK PSEK.41.02 10.20.25.250 ]
[ Sessions NOC-CO Servers andy (Netra t1 105 DNS server) 172.16.18.104 ]
[ Sessions NOC-CO Servers Blackserver 172.16.19.117 ]
[ Sessions NOC-CO Servers Blackserver VNC tunnel 172.16.19.117 ]
[ Sessions NOC-CO Servers Blueserver 172.16.19.115 ]




Mon, 26 Aug 2013 22:31:18 GMT  
 
 [ 26 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Perl 5.001e subroutine returns length of array instead of array

2. How do i process a variable length array

3. last element in a variable length array?

4. Perl 5 arrays of arrays, length of etc..

5. Putting an array of structures in a structure using Class::Template

6. Help, needed to create a 3 tier hash/array structure

7. Need help with perl structure (hash of arrays and hashes)

8. URGENT: Hash in Hash, Array in Array, Array in Hash, Hash in Array

9. Sorting @Array from variable in middle of array

10. hash of arrays using variable array names

11. Arrays and Assoc Arrays as user defined magic variables

12. Help with tree-like data structure (I think)

 

 
Powered by phpBB® Forum Software