Hello gng4life,

Welcome back to Perl, and welcome to the monastery.

You do not show us any code that you have tried to create and resolve your problem and from your description I am not 100% sure what you are trying to do. So I will try to propose something in pseudo code and you can correct me if I am wrong.

From your description I understood that you have one file (File A) with all the data 200 lines.

First step that I would do would be open the file parse it into an array of hashes (why Array Of Hashes) to keep the order of the lines, (why hash) so you can separate the lines based on 3 keys e.g. (host, IP, MAC). After parsing the file you should have 200 array hashes. In case you do not care about the sequence of the lines then simply use Hash Of Hashes (HoH) and use the line number as a key in case of sorting.

Step two, get the files to be processed in an array or in something that you can iterate. Create a loop for each file (assuming 200 files exist) and write a very simple script that will search for string (<HOST_NAME>) after that insert value from hash, search for (<HOST_IP>) insert value and last search for (<HOST_MAC>) insert value. At this point if you manage to write a script to do that you are done... :D

It is not that difficult, start with the basics, post us some code and we will help where ever you get stuck. :D

Minor note, other Monks maybe they will come up with a smaller or bigger solution proposed to your problem, this is just one way of resolving it out of my mind.

Update: I created a bit more advanced answer to your question that might confuse you a bit but I think if you examine it will provide you all the information regarding the first step of my answer.

So what I have created I thought about having multiple files with data in the same format (e.g. file1.txt, file2.txt, etc) and assuming that those multiple files contain your data. I assume that file1.txt is the starting point of your data so I place file1.txt as the last file on script input because inputs are processed in reversed order first is processed the last input and last is the first. What do I mean for example perl test.pl file2.txt file1.txt first file processed is file1.txt and second file processed is file2.txt.

What the script does, first parses each line of the files (one by one) splits the line based on commas (based on the input that you provided us) and creates a hash with the keys that you said (hostname, ip, mac) with hash slice (Slices). Then simply we create a hash of hashes (HASHES OF HASHES) based on (Variables related to filehandles) and also the second level key is the line number of each file. The script is generic and will work with as many files you provide it.

The last part of the script is using the module File::Find::Rule to find all (xml) files in the defined directories that you will provide. Then simply combine the script with the answer of fellow monk haukex and you should be just fine. ;)

#!usr/bin/perl use strict; use warnings; use Data::Dumper; use File::Find::Rule; sub process_file { my %HoH; while (<>) { chomp; my %hash; my @data = split /,/, $_; # split each line on comma my @keys = ('HOSTNAME', 'IP', 'MAC'); @hash{@keys} = @data; # hash slice $HoH{$ARGV}{$.} = \%hash; } continue { close ARGV if eof; } return \%HoH; } sub get_xml_files { my @dirs = ('.'); my $level = shift // 2; my @files = File::Find::Rule->file() ->name('*.xml') ->maxdepth($level) ->in(@dirs); return \@files; } print Dumper process_file(); print Dumper get_xml_files(); __END__ $ perl test.pl file2.txt file1.txt $VAR1 = { 'file2.txt' => { '3' => { 'HOSTNAME' => 'hostf', 'IP' => '6.6.6.6', 'MAC' => '00000C123458' }, '1' => { 'HOSTNAME' => 'hostd', 'MAC' => '00000C123456', 'IP' => '4.4.4.4' }, '2' => { 'HOSTNAME' => 'hoste', 'MAC' => '00000C123457', 'IP' => '5.5.5.5' } }, 'file1.txt' => { '1' => { 'IP' => '1.1.1.1', 'MAC' => '00000C123456', 'HOSTNAME' => 'hosta' }, '2' => { 'HOSTNAME' => 'hostb', 'MAC' => '00000C123457', 'IP' => '2.2.2.2' }, '3' => { 'HOSTNAME' => 'hostc', 'MAC' => '00000C123458', 'IP' => '3.3.3.3' } } }; $VAR1 = [ 'response.xml' ];

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Pulling from a list and inserting into XML documents by thanos1983
in thread Pulling from a list and inserting into XML documents by gng4life

Title:
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, details, 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, summary, 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.