perl hashes question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl hashes question
# 1  
Old 04-09-2008
perl hashes question

hi guys im running into a problem here this is my script
Code:
#!/usr/bin/perl

use CGI qw(:standard);
$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/html\n";
$header .= "\n";

#get the point parameter from nhl.html
$Team = param("points");
print "$header";

open(INFILE, "nhl_data") || die "cannot open file";
%rooms= ("points" => 0);
 while(<INFILE>)
{
  if($_=~/$Team/)
  {
    @fields=split(/:/,$_);
    ++$rooms{"$fields[2]"};
    $rooms{"$fields[2]"} = $rooms{"$fields[2]"} +1;
    print "$rooms{$_}";


  }
}

what im trying to do here is: the script is getting the team name from an html file
then this name is searched on the nhl_data once it finds the team is going to count the goals on that specific team using a sum , my question is: how would i go to that specific field in this case field 3 and add each value accordingly?

nhl_data example
Code:
Sakic, Joe:Colorado Avalanche:617:994:1343
Recchi, Mark:Atlanta Thrashers:518:847:1386
Sundin, Mats:Toronto Maple Leafs:546:755:1288
Modano, Michael:Dallas Stars:523:744:1298
Roenick, Jeremy:San Jose Sharks:504:686:1300
Selanne, Teemu:Anaheim Ducks:540:597:1045
Fedorov, Sergei:Columbus Blue Jackets:469:663:1174
Brind'Amour, Rod:Carolina Hurricanes:427:685:1322
Tkachuk, Keith:St. Louis Blues:490:475:1029
Weight, Doug:Anaheim Ducks:264:700:1118
Chelios, Chris:Detroit Red Wings:183:761:1600
Lidstrom, Nicklas:Detroit Red Wings:209:714:1234
Kariya, Paul:St. Louis Blues:380:534:875
Roberts, Gary:Pittsburgh Penguins:434:469:1193
Linden, Trevor:Vancouver Canucks:372:489:1365
Kovalev, Alex:Montreal Canadiens:358:488:1047
Alfredsson, Daniel:Ottawa Senators:325:506:832
Naslund, Markus:Vancouver Canucks:366:446:1010
Nolan, Owen:Calgary Flames:379:417:1042

any suggestion would be highly appeciated
thanks guys

Last edited by Yogesh Sawant; 04-09-2008 at 01:04 AM.. Reason: added code tags
# 2  
Old 04-09-2008
Your attempt is fairly close. The first thing you should do and do it now and keep doing it, is use "strict" and "warnings" with all your perl programs. There is no need to build an HTTP header manually, the CGI module will handle that for you.

Code:
use strict;
use warnings;
use CGI qw(:standard);
print header();# the CGI module will print the HTTP header using the header function.

my %rooms;

#get the point parameter from nhl.html
my $Team = param("points");

open(INFILE, "nhl_data") or die "cannot open file: $!";
while(<INFILE>){
   my ($team,$points) = (split(/:/))[1,2];#gets the team name and the points only
   if ($Team eq $team) {
      $rooms{$Team} += $points; #add the points using  +=
   }
}
close INFILE;
print $rooms{$Team};


This requires that the input from the html form matches exactly the team name in the file, including spaces and CaSe. Hopefully you have already taken care of that in the html form by creating a menu of some sort that the person using the form will use to pick the team and not just a text box where they write in the name of the team. You could use a regexp to match a team name but than you might get false matches if you are not careful, and judging by the code you posted you are not very familiar with perl.

Is this school work?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl hash of hashes anonymous array

Hello experts. I'm having problems with a snippet of code. I was hoping to get help/advice to correct. A file that this script parses has changed to the point where I can no longer use a scalar, it looks as though I need to create an array for a hash of hashes below. The first output of... (1 Reply)
Discussion started by: timj123
1 Replies

2. Shell Programming and Scripting

Perl : array of hashes help

Hi, I have array of hashes and each key has array like below. @array1 = ( { 'url' => , 'bill' => }, { 'url' => , 'bill' => }, { 'url' => , ... (0 Replies)
Discussion started by: ragilla
0 Replies

3. Shell Programming and Scripting

perl: dereferencing a hash of hashes

Hi there, I am trying to dereference my hash of hashes but post dereferencing, it seems to lose its structure I am using Data::dumper to help me anaylise. This is the code im using to build the HoH, (data comes from a file). I have also performed a Dumper on the data structure before and after... (1 Reply)
Discussion started by: rethink
1 Replies

4. Shell Programming and Scripting

PERL - another quick hash of hashes question

Hi, sorry, two hash related questions in one day .. but this has got me a bit stuck. I have a mysql database table that kind of looks like this, the table is called "view1" and a snippet of that table (SELECT'ing just rows with serial number 0629AN1200) is below serial nic_name ... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

5. Shell Programming and Scripting

PERL - printing a hash of hashes to screen

Hi there I have a hash of hashes made up of the following data bge0|100|half|10.36.100.21 bge1|1000|full|10.36.100.22 bge2|1000|full|10.36.100.23 which when i turn into a hash, would look like this inside the system bge0 -> nic_speed -> 100 nic_duplex -> half ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

6. Shell Programming and Scripting

perl hash of hashes from database

hi there, I have some database output that looks like this SELECT nic_name,nic_duplex,nic_speed,nic_ip FROM network_table WHERE hostname = "server1" result is this (ive delimited with a pipe for ease of reading) bge0|full|1000|10.32.100.1 bge1|full|1000|11.12.101.7 ... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

7. Shell Programming and Scripting

Hashes help in Perl

Hi, I am stuck at this problem where part of my code would store all the websites that has been accessed by a user. I pull these values from a log file. I want to create a HASH of HASHES ? (Please correct me if this is not the right approach) where I would store all the hits to website with... (4 Replies)
Discussion started by: Dabheeruz
4 Replies

8. Shell Programming and Scripting

Perl Hashes, reading and hashing 2 files

So I have two files that I want to put together via hashes and am having a terrible time with syntax. For example: File1 A apple B banana C citrusFile2 A red B yellow C orangeWhat I want to enter on the command line is: program.pl File1 File2And have the result... (11 Replies)
Discussion started by: silkiechicken
11 Replies

9. Shell Programming and Scripting

How to declare hashes in KSH similar to Perl ?

Hi, Is it possible to delcare hashes in KSH the way we do it in Perl. Like I want to declare something like: fruits="Juicy" fruits="healthy" fruits="sour" echo fruits Ofcourse this piece of code does not work in KSH. Please let me know if there is a way of doing it in KSH. ... (2 Replies)
Discussion started by: tipsy
2 Replies

10. Shell Programming and Scripting

perl: hashes, whiles, and last

hello everyone, i am creating 2 hashes from 2 different files, then looking for the value of one in the value of the other to make a new file. for example: file1: DENV => Denver file2: H224-0A-12 => DENVER if Denver is found in DENVER (case insensitive), a new hash now contains H224-0A-12... (0 Replies)
Discussion started by: effigy
0 Replies
Login or Register to Ask a Question