Solved

loop through an array to create a hash list

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

932 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