CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Nesting arrays: too rusty

 



hvdkooij
New User

Jan 6, 2017, 3:46 AM

Post #1 of 5 (3853 views)
Nesting arrays: too rusty Can't Post

I am trying to use an XML file.

The code so far is:


Code
#!/usr/bin/perl 

# Parse Microsofts URL list from XML
# Original source: https://support.content.office.net/en-us/static/O365IPAddresses.xml

use XML::Simple;
use Data::Dumper;

$home = $ENV{"HOME"};
$workdir = "/$home/categories.networkschool.org";
$xmlsource = "$workdir/O365IPAddresses.xml";

$O365 = new XML::Simple;

$O365list = $O365->XMLin($xmlsource);


foreach my $product (keys %{$O365list->{'product'}})
{
print "$product\n";
}

print "$home\n\n";

print Dumper($O365list);


And the output:


Code
OneNote 
OfficeMobile
OfficeiPad
EXO
Planner
Identity
EX-Fed
EOP
RCA
WAC
Office365Video
CRLs
Sway
LYO
SPO
Yammer
ProPlus
o365
/home/hugo1966

$VAR1 = {
'product' => {
'OneNote' => {
'addresslist' => [
{
'address' => [
'13.73.106.160/32',
'13.75.158.234/32',
'13.78.58.132/32',
'13.79.161.29/32',
'13.82.54.72/32',
'13.93.219.105/32',
'40.74.129.10/32',
'40.118.97.54/32',
'52.172.158.178/32',
'104.41.35.170/32',
'104.211.163.139/32'
],
'type' => 'IPv4'
},
{
'address' => [
'*.onenote.com',
'*.msecnd.net',
'*.microsoft.com',
'*.office.net',
'apis.live.net',
'www.onedrive.com',
'www.onenote.com',
'Cdn.optimizely.com',
'Ajax.aspnetcdn.com',
's.ytimg.com',
'www.youtube.com',
'ad.atdmt.com',
'Cdn.onenote.net'
],
'type' => 'URL'
}
]
},

....

Now I want to generate a list op IP adresses/subnets and a list of domains for each $product.

However it seems my perl has become to rusty in order to tackle it.

So a few good hints will be appreciated.


hvdkooij
New User

Jan 6, 2017, 3:59 AM

Post #2 of 5 (3852 views)
Re: [hvdkooij] Nesting arrays: too rusty [In reply to] Can't Post

I got one step closer:


Code
foreach my $product (keys %{$O365list->{'product'}}) 
{
print "$product\n";
$productlist = $O365list->{'product'}{$product}{'addresslist'};
print Dumper ($productlist);
}


Which gives me:

Code
OneNote 
$VAR1 = [
{
'address' => [
'13.73.106.160/32',
'13.75.158.234/32',
'13.78.58.132/32',
'13.79.161.29/32',
'13.82.54.72/32',
'13.93.219.105/32',
'40.74.129.10/32',
'40.118.97.54/32',
'52.172.158.178/32',
'104.41.35.170/32',
'104.211.163.139/32'
],
'type' => 'IPv4'
},
{
'address' => [
'*.onenote.com',
'*.msecnd.net',
'*.microsoft.com',
'*.office.net',
'apis.live.net',
'www.onedrive.com',
'www.onenote.com',
'Cdn.optimizely.com',
'Ajax.aspnetcdn.com',
's.ytimg.com',
'www.youtube.com',
'ad.atdmt.com',
'Cdn.onenote.net'
],
'type' => 'URL'
}
];
OfficeMobile
$VAR1 = {



BillKSmith
Veteran

Jan 6, 2017, 7:33 AM

Post #3 of 5 (3844 views)
Re: [hvdkooij] Nesting arrays: too rusty [In reply to] Can't Post

I think that this is what you want.

Code
#!/usr/bin/perl  
use strict;
use warnings;

# Parse Microsofts URL list from XML
# Original source: https://support.content.office.net/en-us/static/O365IPAddresses.xml

use XML::Simple;
use Data::Dumper;

my $home = $ENV{"HOME"};

#$workdir = "/$home/categories.networkschool.org";
#$xmlsource = "$workdir/O365IPAddresses.xml";

my $O365 = {
'product' => {
'OneNote' => {
'addresslist' => [
{
'address' => [
'13.73.106.160/32', '13.75.158.234/32',
'13.78.58.132/32', '13.79.161.29/32',
'13.82.54.72/32', '13.93.219.105/32',
'40.74.129.10/32', '40.118.97.54/32',
'52.172.158.178/32', '104.41.35.170/32',
'104.211.163.139/32'
],
'type' => 'IPv4'
},
{
'address' => [
'*.onenote.com', '*.msecnd.net',
'*.microsoft.com', '*.office.net',
'apis.live.net', 'www.onedrive.com',
'www.onenote.com', 'Cdn.optimizely.com',
'Ajax.aspnetcdn.com', 's.ytimg.com',
'www.youtube.com', 'ad.atdmt.com',
'Cdn.onenote.net'
],
'type' => 'URL'
}
]
},

},
};

while ( my( $product_name, $product_data ) = each %{$O365->{'product'}} ) {
print $product_name, "\n";
for (0..1) {
my $address_data = $product_data->{addresslist}[$_];
my $address_type = $address_data->{type};
my @address_values = @{$address_data->{address}};
local $" = "\n ";
print " $address_type\n";
print " @address_values\n";
}
}


OUTPUT:
OneNote
IPv4
13.73.106.160/32
13.75.158.234/32
13.78.58.132/32
13.79.161.29/32
13.82.54.72/32
13.93.219.105/32
40.74.129.10/32
40.118.97.54/32
52.172.158.178/32
104.41.35.170/32
104.211.163.139/32
URL
*.onenote.com
*.msecnd.net
*.microsoft.com
*.office.net
apis.live.net
www.onedrive.com
www.onenote.com
Cdn.optimizely.com
Ajax.aspnetcdn.com
s.ytimg.com
www.youtube.com
ad.atdmt.com
Cdn.onenote.net

Good Luck,
Bill


hvdkooij
New User

Jan 9, 2017, 4:00 AM

Post #4 of 5 (3805 views)
Re: [BillKSmith] Nesting arrays: too rusty [In reply to] Can't Post

Thanks Bill.
I will look into this suggestion and let you know the results.
It will most likely need a bit more tinkering but this one is putting me back on track I guess.


BillKSmith
Veteran

Jan 9, 2017, 6:52 AM

Post #5 of 5 (3802 views)
Re: [hvdkooij] Nesting arrays: too rusty [In reply to] Can't Post

The rules for dereferencing references are documented in perldoc perlretut in the section "Using references".

Rule 1 always works. The others are convenient shortcuts. Most of the shortcuts have restrictions. When you are stuct, I recommend that you get your code working with rule 1. Then go back and improve the appearance of your code in small steps. Test each step as you make it.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives