Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Mapping with Hashes

Status
Not open for further replies.

mac9968

IS-IT--Management
Apr 5, 2011
9
0
0
US
Morning all,
I have a task that I need to get done, and cant seem to get it together. I have a file that I am read via the <> operator. This files contains simply:
17 21 19 70 99 45 32 25 67 89 90 91
11 10 12 33 34 36 37

several lines of numbers from 1 to 99. In the perl file I am pulling the file in as I said with <>. I would like to read each number and store and report against a Hash called %regions. I have initialized this hash as %regions = (NYC => 12, KansasCity => 13, Chicago => 11, etc.

I would like to print out each Region and the number of time that $region{item} shows up in the file that is being pulled in.

I hope this makes sense, sorry the length of this one.

Mark
 
What have you tried? Where are you running into problems?
 
rharsh thank you,
I have attached the script i have thus far. I am unable to do the comparative part. Take the input from the file from <> and campare it to the %regions that I have define. After that I would like to report all the regions defined, and the number of times each region showed up in the file from the <>

I dont know where to begin this process of camparing, then reporting.
Code:
#!/usr/bin/perl
use strict;
use warnings;

my %region = ( Relay => 5, Chicago => 70, Satsuma => 11,
Reston => 13, FtWorth => 45, Lenexa => 15, Akron => 17,
Atlanta => 19, Anaheim => 21, Fairfax => 27, Oroville => 25,
Norcross => 29, Westbury => 33, Fresno => 35, NYC => 39, Kansas => 43,
Springfield => 41 );

my (%query);
while ( defined(my $line = <> ))
{
	my ($query) = split " ", $line, 1;
	$query{item} = $query;
	print $query
}
 
Your region has is set up backwards, if you are storing "5" in your query hash, then you want your region hash to be 5 => Relay so you can then print, I only fixed that one for this example.

Code:
use strict;

my $data = "17 21 19 70 99 45 32 25 67 89 90 91 11 10 12 33 34 36 37";

my %region = ( 5 => 'Relay', Chicago => 70, Satsuma => 11,
Reston => 13, FtWorth => 45, Lenexa => 15, Akron => 17,
Atlanta => 19, Anaheim => 21, Fairfax => 27, Oroville => 25,
Norcross => 29, Westbury => 33, Fresno => 35, NYC => 39, Kansas => 43,
Springfield => 41 );

my %count;
my @list = split /\s*/, $data;

for my $line (@list) {
	$count{$line}++;
}

for my $c (keys %count){
	print "$c $count{$c} $region{$c}\n";
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I should add there is probably cleaner way to do this, but this was quick and simple and I'm on 5 hours sleep :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks Travis,
Please get some sleep, I will give this a try.
 
That did it, I change a few things, but this should work. I will format the print a bit, but thank again Travis. I also added the rest of my regions, and code for the <> operator.
Code:
#!/usr/bin/perl
use warnings;
use strict;

my %region = ( 5 => 'Relay', 70 => 'Chicago', 11 => 'Satsuma',
13 => 'Reston', 45 => 'FtWorth', 15 => 'Lenexa', 17 => 'Akron',
19 => 'Atlanta', 21 => 'Anaheim', 27 => 'Fairfax', 25 => 'Orville',
29 => 'Norcross', 33 => 'Westbury', 35 => 'Fresno', 39 => 'NYC', 43 => 'Kansas',
41 => 'Springfield', 47 => 'NV-ALU-Harrion-01', 49 => 'NV-ALU-Harrision-02', 65 => 'NV-SAM-Chicago',
2 => 'NV-SAM-Akron', 44 => 'NV-ERI-Kansas', 51 => 'NV-SAM-Tacoma', 52 => 'NV-SAM-Bayamon', 61 => 'NV-ERI-Miami',
53 => 'NV-ALU-Burbank-01', 54 => 'NV-ALU-Burbank-02', 55 => 'NV-ALU-Anaheim', 56 => 'NV-ERI-Orland', 57 => 'NV-ERI-Houston-01',
58 => 'NV-ERI-Houston-02', 64 => 'NV-SAM-Stockton', 60 => 'NV-SAM-Buffalo-01', 63 => 'NV-ERI-Atlanta', 62 => 'NV-ALU-Fairfax',
66 => 'NV-SAM-Buffalo-02', 67 => 'NV-ERI-Nashville', 68 => 'NV-ERI-FtWorth', 69 => 'NV-SAM-Buffalo-03', 71 => 'NV-ALU-Elkridge-01',
72 => 'Elkridge-02', 75 => 'NV-SAM-Cheyenne', 76 => 'NV-SAM-Omaha', 77 => 'NV-ALU-Springfield-01', 78 => 'NV-ALU-Springfield-02',
98 => 'Orlando', 99 => 'Detriot');

my %count;
while ( defined( my $line = <>))
{
chomp $line;
my @list = split /\s/, $line;

for my $line (@list) {
        $count{$line}++; }
}
for my $cc (keys %count) {
        print "$cc $count{$cc} $region{$cc}\n";
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top