
Real Beginner
--0015174bf0cc7b5ff204a1726543
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
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 al=
l
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 i=
s
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
use strict;
use 5.010;
sub greet {
state [at] people;
my $name =3D shift [at] _;
print "Hi $name! ";
if ( [at] people){
my [at] peoplec =3D [at] people;
my $lastperson =3D shift [at] peoplec;
my $beforelast =3D shift [at] peoplec;
print "I've seen: ";
foreach ( [at] peoplec){
print "$_, ";}
print "$beforelast and ";
print"$lastperson!\n";
}
else {
print "You are the first here!\n";
}
push [at] people, $name;
}
&greet ("Fred");
&greet ("Barney");
&greet ("Wilma");
&greet ("Betty");
&greet ("Bamm-Bamm");
exit;
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 b=
e
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.
Cheers,
Tiago
--
"Education is not to be used to promote obscurantism." - Theodonius
Dobzhansky.
"Gracias a la vida que me ha dado tanto
Me ha dado el sonido y el abecedario
Con =E9l, las palabras que pienso y declaro
Madre, amigo, hermano
Y luz alumbrando la ruta del alma del que estoy amando
Gracias a la vida que me ha dado tanto
Me ha dado la marcha de mis pies cansados
Con ellos anduve ciudades y charcos
Playas y desiertos, monta=F1as y llanos
Y la casa tuya, tu calle y tu patio"
Violeta Parra - Gracias a la Vida
Tiago S. F. Hori
PhD Candidate - Ocean Science Center-Memorial University of Newfoundland
--0015174bf0cc7b5ff204a1726543--
Re: Real Beginner
On Thu, Apr 21, 2011 at 04:33:27PM -0230, 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 certainly hope you don't get slammed, but I understand the
trepidation.
> 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:
Take a look at the join() function:
$ perldoc -f join
That will get you most of the way there, though you'll probably still
need to do something special for the "and" part.
Also, be careful if you've only seen a single person before.
> I am no perl expert, but this code looks really clunky to me, so I was just
> looking for some input.
Your code isn't bad at all. The indentation is wrong, but that might be
some problem with mailing it. Come back to the list if these hints are
insufficient.
Good luck.
--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Real Beginner
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 [at] people;
>
> my $name = shift [at] _;
>
> print "Hi $name! ";
You're missing a "\n".
>
> if ( [at] people){
>
> my [at] peoplec = [at] people;
>
> my $lastperson = shift [at] peoplec;
you should separate words in identifiers using underscores -
my $last_person
>
> my $beforelast = shift [at] peoplec;
>
Ditto.
> print "I've seen: ";
>
> foreach ( [at] peoplec){
>
> print "$_, ";}
>
> print "$beforelast and ";
>
> print"$lastperson!\n";
>
> }
>
> else {
>
> print "You are the first here!\n";
>
> }
>
> push [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Real Beginner
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'm a beginner, too. I've worked through this chapter somewhat
recently, so my advice shouldn't be too hard to understand :-)
Hopefully _I_ don't get slammed!
> I was trying to do exercise 5 of the 5th edition of learning perl:
>
>
You forgot to include "Chapter 4" but that's ok, it wasn't hard to find
based on the context since I had the book right here :-)
I'll snip most of your code to the part that I think may be what's
slowing you down.
>
> my [at] peoplec = [at] people;
>
> my $lastperson = shift [at] peoplec;
>
> my $beforelast = shift [at] peoplec;
>
>
>
I think I see what you did, but you can probably make it easier by
taking advantage of how 'print' handles arrays as a whole. An example:
#!/usr/bin/perl -wl
use strict;
my [at] designations = ( 'red', 'two', 'rogue', 'leader' );
print "my array contains: [at] designations";
# displays the following....
# my array contains: red two rogue leader
Then you shouldn't have to iterate through each item of the array, or
'shift' off items from a duplicated array. The problem didn't include
commas in the output, which (for me), signaled that I could probably
just 'print' the entire array at once.
>
>
>
> 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.
>
>
I hear ya! Keep trying :-)
Brian
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Real Beginner
----- Original Message -----
From: "Shlomi Fish" <shlomif [at] iglu.org.il>
To: <beginners [at] perl.org>
Cc: "Tiago Hori" <tiago.hori [at] gmail.com>
Sent: Thursday, April 21, 2011 3:01 PM
Subject: Re: Real Beginner
> On Thursday 21 Apr 2011 22:03:27 Tiago Hori wrote:
>>
>> print "Hi $name! ";
>
> You're missing a "\n".
Naw, the original post indicates that they want all the output on a single
line. The return comes later...
.... either here:
>> print"$lastperson!\n";
.... or here:
>> print "You are the first here!\n";
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Real Beginner
----- Original Message -----
From: "Tiago Hori" <tiago.hori [at] gmail.com>
To: <beginners [at] perl.org>
Sent: Thursday, April 21, 2011 2:03 PM
Subject: Real Beginner
> I was trying to do exercise 5 of the 5th edition of learning perl:
....
> 4. [10] Write a subroutine, named greet, that welcomes the person you name
> by telling them the name of the last person it greeted:
....
> 5. [10] Modify the previous program to tell each new person the names of
> all
> of the people it has previously greeted:
....
> 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
>
>
> use strict;
>
> use 5.010;
>
>
> sub greet {
>
> state [at] people;
>
> my $name = shift [at] _;
>
> print "Hi $name! ";
>
> if ( [at] people){
>
> my [at] peoplec = [at] people;
>
> my $lastperson = shift [at] peoplec;
>
> my $beforelast = shift [at] peoplec;
>
> print "I've seen: ";
>
> foreach ( [at] peoplec){
>
> print "$_, ";}
>
> print "$beforelast and ";
>
> print"$lastperson!\n";
>
> }
>
> else {
>
> print "You are the first here!\n";
>
> }
>
> push [at] people, $name;
>
> }
>
> &greet ("Fred");
> &greet ("Barney");
> &greet ("Wilma");
> &greet ("Betty");
> &greet ("Bamm-Bamm");
>
> exit;
>
> 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).
One quick alternative to the original problem would be to replace your loop
logic with a simple print of the array as a string (" [at] peoplec"); or, if you
want to insert commas, via the join() function.
http://perldoc.perl.org/functions/join.html
(Though I don't know whether that would be jumping ahead, relative to where
you are in the book. i.e. Was the exercise intended to learn more about
looping?)
Here are a few thoughts relative to what you have so far (again, without
knowing what tools you are "authorized" to use)...
- In shift-ing the names for comparison, what if you're the 2nd visitor?
(i.e. there has only been one other visitor but you're currently trying to
pull two names from the visitors list) Depending on how you construct your
conditional logic, you would really only need to pull one name from the
list.
- Would the existing method of shift-ing the names off the [at] people array
result in a report of visitors in the order desired? (see: pop() function:
http://perldoc.perl.org/functions/pop.html)
- Rather than looping, you could simply use a join() on your [at] peoplec array,
after pop()-ing off the last visitor, and then just print out the last
visitor prefixed with " and ."
- Rather than pulling and storing the names of the visitors for comparison,
you could use available array parameters (number of elements) to index your
way through the original [at] people variable, via a for loop. (There would no
longer be a need for the second [at] peoplec array.)
e.g. without over-supplying...
for (my $inx=1; $inx <= $#people; $inx++){
if ($inx < $#people){
Moving beyond a simple blast of previous visitors, possibly taking the
project in a direction not explored in the book, does the wording of the
problem require you to process the list of previous visitors to remove
duplicates, and does the problem require display of the names alpahbetically
or in ascending/descending order of their visits?
- How to reverse the listing of previous visitors, first-to-last and
last-to-first?
- How well formatted should the display of visitors be? Can it just be one
long line, regardless of the number of past visitors, or should it be
formatted to look a bit nice, with a maximum length and a continuation
indent?
- What if you want to see a list of previous individuals without
duplication?
- What if you want to insure that each new person is uniquely identifiable?
Stop reading here.
////////////////////////////
///// spoiler alert //////
////////////////////////////
Below are the examples I played with to refresh my memory. One a slight
modification of the original posted, one using join, and the last using
array indexing.
sub greet_origtweak {
state [at] people;
my $name = shift [at] _;
print "Hi $name! ";
if ( [at] people)
{
my [at] peoplec = [at] people;
my $lastperson = pop [at] peoplec;
print "I've seen: ";
if ( [at] peoplec)
{
print shift [at] peoplec;
print ", $_" foreach ( [at] peoplec);
print " and ";
}
print"$lastperson!\n";
}
else
{
print "You are the first here!\n";
}
push [at] people, $name;
}
sub greet_join {
state [at] people;
my $name = shift [at] _;
print "Hi $name! ";
if ( [at] people)
{
my [at] peoplec = [at] people;
my $lastperson = pop [at] peoplec;
print "I've seen: ";
print join(", ", [at] peoplec) . " and " if ( [at] peoplec);
print "$lastperson!\n";
}
else
{
print "You are the first here!\n";
}
push [at] people, $name;
}
sub greet_index {
state [at] people;
my $name = shift [at] _;
print "Hi $name!";
if ( [at] people)
{
print " I've seen: $people[0]";
for (my $inx=1; $inx <= $#people; $inx++)
{
if ($inx < $#people)
{
print ", ";
}
else
{
print " and ";
}
print $people[$inx];
}
print "!\n";
}
else
{
print " You are the first here!\n";
}
push [at] people, $name;
}
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Real Beginner
From: Tiago Hori <tiago.hori [at] gmail.com>
> Hey Guys,
>
> I am a real beginner so at the risk of being slammed by some, I wanted to
> get some input.
Don't worry, there are people here that will protect you and shout you were slammed even if you do not feel hurt at all.
> 5. [10] Modify the previous program to tell each new person the
> names of all of the people it has previously greeted:
If you did not insist on the "and" between the last and one but last
you could just use the join() builtin:
print "I've seen ", join( ", ", [at] people), "\n";
if you do want the "and", you need to do something more. Accessing
the last element of an array is easy. It's either $people[ $#people ]
or $people[ -1 ]. (Yes, you can index from the end with -1 being the
last element, -2 the one before, etc.)
To access all except the last you need to use something called "array
slice". If you use [at] instead of $ in front of the variable name you
may specify several indexes within the [ ] and get a list of array
elements with those indexes. You can even use the range operator ..
to specif a range of indexes.
So all element except the last element would be [at] people[ 0 ..
$#people-1 ].
You can then use the join() to put commas between the elements from
first to the last-but-one and then add the " and " and the last
element:
print "I've seen ", join( ", ", [at] people[ 0 .. $#people-1]), " and ",
$people[-1], "\n";
This will of course work correctly only if there are at least two
elements in the [at] people array but testing that and printing the
single element is easy :-)
> &greet ("Fred");
Drop the &!
In most cases it doesn't change a thing, but in some cases it can
lead to hard to find bugs!
If you do specify the parameters, then you've just bypassed the
prototype on that subroutine (but there's hardly ever any so it
doesn't matter much), but if you do not specify any parameters it
will NOT call the subroutine with no parameters, but rather with the
current parameters. See
sub inner { print "My parameters were: '", join("', '", [at] _), "'\n" }
sub outer {
inner(1, 2, 3);
inner();
inner;
&inner(1, 2, 3);
&inner();
&inner;
}
outer( "Hello", "world!");
> 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).
Conversely to what some people make you believe you are very unlikely
to be told your code sucks without being told why on this list.
Jenda
P.S.: Please use meaningfull subjects. "Real Beginner" doesn't say
much.
===== Jenda [at] Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Real Beginner
Thanks everybody. This gives me a lot of material to work on and think about=
.. I am likely to come back with more questions as I move further into the bo=
ok.
Cheers, T. =
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/