Solved

loop through an array to create a hash list

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now