Hi Tiago, Welcome aboard.
I'll supply some comments on how to improve your code: On Thursday 21 Apr 2011 22:03:27 Tiago Hori wrote: > Hey Guys, > > I am a real beginner so at the risk of being slammed by some, I wanted to > get some input. > > I was trying to do exercise 5 of the 5th edition of learning perl: > > It is based on exercise 4, so I will paste both here: > > 4. [10] Write a subroutine, named greet, that welcomes the person you name > by > > telling them the name of the last person it greeted: > > > greet( "Fred" ); > > greet( "Barney" ); > > This sequence of statements should print: > > Hi Fred! You are the first one here! > > Hi Barney! Fred is also here! > > 5. [10] Modify the previous program to tell each new person the names of > all of the > > people it has previously greeted: > > greet( "Fred" ); > > greet( "Barney" ); > > greet( "Wilma" ); > > greet( "Betty" ); > > This sequence of statements should print: > > Hi Fred! You are the first one here! > > Hi Barney! I've seen: Fred > > Hi Wilma! I've seen: Fred Barney > > Hi Betty! I've seen: Fred Barney Wilma > > > > So I worked on it for a while and got something really close to what is in > the answers, but what you get as an output is grammatically incorrect, it > is missing the commas and such. I figured that in reality, eventually in > the real world, I would want the output to be correct or at least tab > delimited. So I went about trying to get commas in and I came up with > this: > > > ! /usr/bin/perl This should be "#!/usr/bin/perl". > > > use strict; > > use 5.010; > Also add "use warnings;". > > sub greet { > > state @people; > > my $name = shift @_; > > print "Hi $name! "; You're missing a "\n". > > if (@people){ > > my @peoplec = @people; > > my $lastperson = shift @peoplec; you should separate words in identifiers using underscores - my $last_person > > my $beforelast = shift @peoplec; > Ditto. > print "I've seen: "; > > foreach (@peoplec){ > > print "$_, ";} > > print "$beforelast and "; > > print"$lastperson!\n"; > > } > > else { > > print "You are the first here!\n"; > > } > > push @people, $name; > > } As Paul Johnson noted the indentation is wrong. > > > > &greet ("Fred"); > > &greet ("Barney"); > > &greet ("Wilma"); > > &greet ("Betty"); > > &greet ("Bamm-Bamm"); > See: https://www.socialtext.net/perl5/subroutines_called_with_the_ampersand > > exit; What is the use of this "exit;" statement? > > I am no perl expert, but this code looks really clunky to me, so I was just > looking for some input. If your input is your code sucks without any > constructive suggestion, please keep it for yourself, since I already know > that my code sucks! :P I am beginner, that's what newbies do, they suck (in > general, some people are brilliant and don't, not my case). > > And since I got writing I thought I would chip in the discussion as a real > newbie, from a newcomer perspective. I am a scientist, brutal criticism is > part of my job (both giving it and receiving it), and I personally find it > never gets easier. I agree with many people that if you want to be great, > there is no other way, you have to, as Randall said, get a thick skin. > However, some people are not learning perl to be great, they just want to > be OK. I am one of those. I have been using rudimentary Bio perl scripts > to deal with Genomic data for a while, but never really learned the > language, so I decided to go back to the beginning and learn it from > there, from the ground. I have to say this list is a bit daunting and > does not look like a beginners list at all. I think it, as Cat Stevens > would say, a wild world out there, but some of us are just trying to get > by. > > That's my view from outside, really outside. > OK, thanks for your commentary. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ "The Human Hacking Field Guide" - http://shlom.in/hhfg What is is. Perceive It. Integrate it. Act on it. Idealize it. -- Leonard Peikoff Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/