Solved

loop through an array to create a hash list

Posted on 2014-04-26
6
244 Views
Last Modified: 2014-05-23
Hello Experts,
 
I have a perl script sub that currently reads a file (attached) and creates a hash list from the file.

# build cidr hash list
open(file, "cidrList.csv");
my @cidrRecs = <file>;
close(file);

foreach my $rec (@cidrRecs) {
	chomp($rec);
	my ($cidrMask, $cidrNum) = split(/,/,$rec);
	$cidrList{$cidrMask} = $cidrNum;
}

Open in new window


What i would like to do is build the hash list from a multidimensional array I create in the script but I can't figure out how to loop through each array within the array to create the hash list. here is what i have so far but it doesn't work.
my @cidrRecs = (
			['255.0.0.0,8' ],
			['255.128.0.0,9'],
			['255.192.0.0,10'],
			['255.224.0.0,11'],
			['255.240.0.0,12'],
			['255.248.0.0,13'],
			['255.252.0.0,14'],
			['255.254.0.0,15'],
			['255.255.0.0,16'],
			['255.255.128.0,17'],
			['255.255.192.0,18'],
			['255.255.224.0,19'],
			['255.255.240.0,20'],
			['255.255.248.0,21'],
			['255.255.252.0,22'],
			['255.255.254.0,23'],
			['255.255.255.0,24'],
			['255.255.255.128,25'],
			['255.255.255.192,26'],
			['255.255.255.224,27'],
			['255.255.255.240,28'],
			['255.255.255.248,29'],
			['255.255.255.252,30'],
			['255.255.255.254,31'],
			['255.255.255.255,32']
			);

foreach my $row (@$cidrRecs) {
	foreach my $cell (@$row) {
		my ($cidrMask, $cidrNum) = split(/,/,$cell);
		$cidrList{$cidrMask} = $cidrNum;
	}
}

Open in new window


Any help is appreciated,
Thanks
cidrList.csv
0
Comment
Question by:romlig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40025117
Why is @cldrRecs a multi-dimensional array?  Since each sub-array has a single string value, there's no point.

If that is what you want to do, this should do it...
%cldrList = map { split /,/, $_->[0] } @cldrRecs

Open in new window

0
 
LVL 2

Expert Comment

by:vogen
ID: 40050530
Is this what you wanted to achieve?

## define empty hash
my %cidrList = ();

## loop through array
foreach my $row(@cidrRecs) {
        ## split to cidrMask and cidrNum
      my ($cidrMask, $cidrNum) = split /,/, shift $row;

        ## add cidrMask as key, and cidrNum as value
      $cidrList{$cidrMask} = $cidrNum;
}
0
 

Author Comment

by:romlig
ID: 40050597
had to tweak it a bit but this was the closest to what i needed thx
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40051204
What did not work with my solution?
0
 
LVL 2

Expert Comment

by:vogen
ID: 40055104
Hi romlig, using map function also give same output. wilcoxon solution is also right except typo in array list name

http://www.xaprb.com/blog/2006/10/05/five-great-perl-programming-techniques-to-make-your-life-fun-again/
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question