From: Tiago Hori <tiago.h...@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( ", ", @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 @ 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 @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( ", ", @people[ 0 .. $#people-1]), " and ", $people[-1], "\n"; This will of course work correctly only if there are at least two elements in the @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("', '", @_), "'\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. ===== je...@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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/