[Buffalo-pm] Learning Perl

Shankar, Ganesh Ganesh.Shankar at RoswellPark.org
Thu Sep 2 16:18:56 CDT 2004


Two items.
One:  The proposed solution to my syntax error was the correct one and after a couple of other changes, the program did what I wanted.  So, thanks to all who replied.

Two:  I'm learning perl from the "Beginning Perl for Bioinformatics" book with tadpoles on the cover.  The examples in the book are close to the problems I face with data analysis.  I read, get the idea, code for my problem, run, fix compile problems.  When, I'm stuck, I google the error message and usually get some relevant hits.  So, my knowledge is very spotty.  This was the first time I used hashes, for example.  Now that I know about them, I can see how I need to use them to store information needed in different parts of the program.  I'm totally non-partisan about my pedagogical sources.

Again, thanks to everyone.

-Ganesh

-----Original Message-----
From: buffalo-pm-bounces at mail.pm.org
[mailto:buffalo-pm-bounces at mail.pm.org]On Behalf Of
buffalo-pm-request at mail.pm.org
Sent: Thursday, September 02, 2004 1:00 PM
To: buffalo-pm at mail.pm.org
Subject: Buffalo-pm Digest, Vol 15, Issue 2


Send Buffalo-pm mailing list submissions to
	buffalo-pm at mail.pm.org

To subscribe or unsubscribe via the World Wide Web, visit
	http://mail.pm.org/mailman/listinfo/buffalo-pm
or, via email, send a message with subject or body 'help' to
	buffalo-pm-request at mail.pm.org

You can reach the person managing the list at
	buffalo-pm-owner at mail.pm.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Buffalo-pm digest..."


Today's Topics:

   1. Re: Syntax error at hash assignment. (Daniel Magnuszewski)
   2. Where to post perl programming question? (James Keenan)
   3. Re: Where to post perl programming question?
      (Jason Parker-Burlingham)


----------------------------------------------------------------------

Message: 1
Date: Wed, 1 Sep 2004 11:37:27 -0700 (PDT)
From: Daniel Magnuszewski <dmagnuszewski at yahoo.com>
Subject: Re: [Buffalo-pm] Syntax error at hash assignment.
To: "Shankar, Ganesh" <Ganesh.Shankar at RoswellPark.org>,
	buffalo-pm at mail.pm.org
Message-ID: <20040901183727.53442.qmail at web21123.mail.yahoo.com>
Content-Type: text/plain; charset="us-ascii"

Perhaps providing an example file would help too - assuming Kevin's resolution does not fix your error completely. 
 
Either way, here's a little hint to help keep your code a bit "cleaner". On the line:
 
( my $student, my $grade ) = split( '\t', $line );
 
You can change it to:
 
my ($student, $grade) = split( '\t', $line );
 
It may not seem like much of a difference in this example, but IMHO when you start having more variables, your lines of code could get long and ugly looking. 
 
...I know, I know...it's petty ;-)
 
-Dan

"Shankar, Ganesh" <Ganesh.Shankar at RoswellPark.org> wrote:
I'm new to perl and learning by running examples. I'm using a script from Programming Perl, 2nd edition, 2nd chapter. There's a script involving getting the average grades of students (http://www.oreilly.com/catalog/pperl2/excerpt/ch01.html#PERL2-CH-1-SECT-3). 
Essentially students are listed in the first column and their scores are listed in the second column. The student can be listed multiple times in the first column. The script should gather all the scores for each student and average them. Then print out the student's name once and their corresponding average.

I have to do something analogous in my microarray data analysis. I copied the script, added strict and warning, assigned scope to the variables and ran into a syntax problem on this line: my $grades{$student} .= $grade . " ";. I'm trying to figure out how the program works but I can't see the syntax error. 

Thanks for any help.



#!/usr/bin/perl
use strict;
use warnings;

open( GRADES, "grades" ) or die "Can't open grades: $!\n";
while ( my $line = ) {
( my $student, my $grade ) = split( '\t', $line );
my $grades{$student} .= $grade . " ";
}

foreach $student ( sort keys my %grades ) {
my $scores = 0;
my $total = 0;
my @grades = split( '\t', $grades{$student} );
foreach $grade (@grades) {
$total += $grade;
$scores++;
}
my $average = $total / $scores;
print "$student: $grades{$student}\t Average: $average\n";
}


_______________________________________________
Buffalo-pm mailing list
Buffalo-pm at mail.pm.org
http://mail.pm.org/mailman/listinfo/buffalo-pm


		
---------------------------------
Do you Yahoo!?
Win 1 of 4,000 free domain names from Yahoo! Enter now.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/archives/buffalo-pm/attachments/20040901/7a2adb7f/attachment-0001.htm

------------------------------

Message: 2
Date: Wed, 1 Sep 2004 18:08:35 -0400
From: James Keenan <jkeen at verizon.net>
Subject: [Buffalo-pm] Where to post perl programming question?
To: buffalo-pm at mail.pm.org
Message-ID: <784473FA-FC63-11D8-A79F-000D932B9CD4 at verizon.net>
Content-Type: text/plain; charset=US-ASCII; format=flowed


On Sep 1, 2004, "Shankar, Ganesh" <Ganesh.Shankar at RoswellPark.org> 
wrote:

> I'm new to perl and learning by running examples. I'm using a script 
> from Programming Perl, 2nd edition, 2nd chapter.

While it *is* possible to teach yourself Perl from Programming Perl 
(a.k.a. the "Camel book"), you should understand:
(a) you're not using the most recent edition (although the differences 
between the 2nd and 3rd editions are probably few from the point of 
view of a beginner), and (more importantly),
(b) while the Camel book is the standard reference work on the 
language, other books are more focused on enabling you to *learn* Perl. 
  The most frequent recommendations:  "Learning Perl" (3rd ed.) by 
Randal L Schwartz and Tom Phoenix (a.k.a. the "Llama book"); "Effective 
Perl Programming" by Joseph N Hall and Randal L Schwartz; and "Elements 
of Programming with Perl" by Andrew L Johnson.

I worked my way through the Llama book when I was first learning Perl, 
and then I turned around and assigned it as a textbook when I was 
teaching an introductory level course.

Jim Keenan



------------------------------

Message: 3
Date: Wed, 01 Sep 2004 22:18:38 -0400
From: Jason Parker-Burlingham <jasonp at panix.com>
Subject: Re: [Buffalo-pm] Where to post perl programming question?
To: buffalo-pm at mail.pm.org
Message-ID: <87sma1l8g1.fsf at freezer.burling>
Content-Type: text/plain; charset=us-ascii

James Keenan <jkeen at verizon.net> writes:

> While it *is* possible to teach yourself Perl from Programming Perl
> (a.k.a. the "Camel book"), you should understand:

> (b) while the Camel book is the standard reference work on the
> language, other books are more focused on enabling you to *learn*
> Perl.

I would add also Damian Conway's wonderful "Object Oriented Perl",
from Hanning.

jason


------------------------------

_______________________________________________
Buffalo-pm mailing list
Buffalo-pm at mail.pm.org
http://mail.pm.org/mailman/listinfo/buffalo-pm

End of Buffalo-pm Digest, Vol 15, Issue 2
*****************************************



More information about the Buffalo-pm mailing list