Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

loop through an array to create a hash list

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
romlig
Asked:
romlig
  • 2
  • 2
1 Solution
 
wilcoxonCommented:
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
 
vogenCommented:
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
 
romligAuthor Commented:
had to tweak it a bit but this was the closest to what i needed thx
0
 
wilcoxonCommented:
What did not work with my solution?
0
 
vogenCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now