Re: How NOT to do it
by dws (Chancellor) on Mar 18, 2001 at 04:23 UTC
|
Much as I like to believe that some people's behavior can be turned around when gently shown the errors of their ways, there are indeed some people who you should never, never let near a project that you're responsible for.
Before writing someone off, though, it's wise to check a few things out:
- Did this person come out of an environment that rewarded wacky code? (Any "reward" here is usually some peer-level cred thing.)
- Is this person bored? Bored people can wreak as much havoc as incompetent people, but they tend to be better at getting the havoc un-wreaked.
- Is this person working outside of their area of competence? (This never, every happens to anyone here. Right? :)
- Does this person have something going on in their outside life that's affecting their behavior on the project? They may think they're keeping a lid on things. (I've been here a few times.)
There's probably hope for anyone who falls into one of these categories. Often the mere fact of pointing out the consequences of their behavior is enough to either turn them around or have them send themselves on the way.
Then there are cretins -- people who leave their Perl golf practice in the code base, or worse. One strategy I've found for cretins is to wait until a headhunter calls, and then refer the cretin to the headhunter.
| [reply] |
Re: How NOT to do it
by merlyn (Sage) on Mar 18, 2001 at 09:20 UTC
|
You know, I think I'm to blame for this. The Llama book never mentions the length operator at all, and that was by design. Almost any time I've seen people need the length of the string, they were about to cycle through the string character-by-character (as you might need to do in other languages), and therefore
miss the more natural and powerful regular expression operations. Since I teach regex very early on, there's usually no need for length.
The hardest part about a tutorial book is coming up with a teachable, self-consistent, useful subset of the language. Many decisions to be made.
-- Randal L. Schwartz, Perl hacker | [reply] |
|
NO, I disagree! I think that there is a certain level of
reasonability to be expected here. If the person in question really thinks
about it - Perl's origins stem from the need to crunch through tomes
of text. There stands to reason that our traditional string functions
like lenght, substring, etc., would be in the language right
from the get go!
I think the hardest part about tutorials, teaching students,
or developing staff is fostering the correct foundations of
common sense.
Maybe what we need is a tutorial on how to RTFM. That was
one skill I learned early on in my career. :)
| [reply] |
|
So what is the shortest way we can rewrite this code?
Kickstart
| [reply] |
|
| [reply] |
|
| [reply] |
(ar0n) Re: How NOT to do it
by ar0n (Priest) on Mar 18, 2001 at 06:20 UTC
|
What makes it even worse, is that he isn't getting the correct length. He's getting the length of the string minus one, since $#temp returns the index of the last element of the array.
Sheez...
[ar0n]
| [reply] [d/l] |
Re: How NOT to do it
by grinder (Bishop) on Mar 19, 2001 at 13:49 UTC
|
Do you honestly think that Perl does NOT have a function to obtain the length of a
string? Do you know ANY modern language that does not have such a function?
Um, SQL?
--
g r i n d e r
| [reply] |
|
| [reply] |
|
Obviously a Windoze Axe-S guru. Racle SQL has had a length function since at least version 6
| [reply] |
|
Actually no, Sybase.
You actually made me go look at the documentation. Turns out that there does exist such
a function in Sybase, but it's called char_leng (talk about obvious).
In any event, there are length
functions in the various SQL dialects, but there is no such beast in the SQL standard
(although it's been years since I've tracked the standard closely, SQL-92 is still pretty
new-fangled for me). So much for portability.
-- g r i n d e r
| [reply] |
|
|
|
Re: How NOT to do it
by t0j0 (Novice) on Mar 19, 2001 at 13:29 UTC
|
if from your snippet shows that that there's a space in the split
@temp=split / /, $string;
instead of
@temp=split //, $string;
maybe what he wanted to do is to count number of words in the string.. which can also be done like this
$ths = "this is a string";
$i=split(/\s/,$ths);
print $i;
| [reply] [d/l] [select] |
|
That is not what the original code snippet does (it does in fact
count the string length .. hence the variable named $string_length).
If you pasted the code into your editor you could see if there was a
space in the regex or not (not). Your code to count words, however, not
only isn't -w compliant (implicit split to @_), it can very easily
give wrong results:
$ths = ' this is a string';
$i = split(/\s/,$ths);
print "$i\n";
If you want to split a string on whitespace, use the special case of
split(" ", $ths) which splits on multiple whitespace and
ignores any leading null field (see: perlfunc:split for details).
$_ = ' this is a string';
my @words = split " ";
print scalar @words, "\n";
# or you could go for this version :-)
$_ = ' this is a string';
my $words =()= /(\S+)/g;
print $words, "\n";
| [reply] [d/l] [select] |