Solved

loop through an array to create a hash list

Posted on 2014-04-26
6
246 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 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