×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Data Mapping with Hashes

Data Mapping with Hashes

Data Mapping with Hashes

(OP)
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

RE: Data Mapping with Hashes

What have you tried? Where are you running into problems?

RE: Data Mapping with Hashes

(OP)
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
} 

RE: Data Mapping with Hashes

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;

RE: Data Mapping with Hashes

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;

RE: Data Mapping with Hashes

(OP)
Thanks Travis,
Please get some sleep, I will give this a try.

RE: Data Mapping with Hashes

(OP)
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";
        } 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close