[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Merging elements



On Thu, Oct 12, 2000 at 04:48:08AM +0800, robinmcf@altern.org wrote:
> Before reading on , please take note of 1 point -I didn't write it in a
> more compact form as the poster probably hasn't been using Perl very
> long.

I didn't make any comments on the compactness of the script.  I have no
objection to the explicitness of the code.  My objection is to the method
the code uses to solve the problem.


> To put it quite bluntly Mr Kimball, you don't seem to  remember what it was
> like to learn.

Learning is fine.  I don't see what that has to do with providing a
solution that is far more complicated than necessary and is based on a
questionable assumption about the data.


> >> $in=~tr/ //s; #take out any unwanted extra spaces
> >> @raw_data = split(/\s/,$in); #load the data into an array for manipulation
> 
> Ronald J Kimball wrote:
> >What's wrong with split(' ', $in)??
> 
> using the data as it was given, a simple split won't work as there are
> extra spaces in the data :-).  

split(' ', $in) is not a simple split.  It is actually the same as
split(/\s+/, $in), except that split(' ', $in) will discard a leading null
field.  This is a special feature of split, perfect for just this case.


> Secondly so the poster can see where the
> data is going  I purposely don't use implicit $_ or @_ throughout the script

I didn't make any comments on that either.


> >Why are you going to all the trouble of creating a hash with the keys and
> >values backwards, when you're going to end up reversing the hash anyway?
> >
> >Especially when you're basing your assumption that all the values are
> >unique on a very small sample of input data.
> 
> from the small snippet of code, neither  you nor I know what the greater
> picture is, based on what is under your nose however the PERL approach to
> dealing with this situation - ie using a HASH to extract unique elements
> from a list - is clear.

Using a hash is clear.  Using two hashes, with the first backwards and
depending on an assumption that the values are unique, then iterating over
the first hash to create the second hash in exactly the way you could have
created that hash in the first place, is not clear at all.


> >I would not recommend using this approach to solve the problem.
> 
> just as I wouldn't recommend your approach to a newbie learning the trade
> :-), but rather as an example of nicely turned code for those who
> understand the it. <SMILE>It all depends on your motivations for posting,
> doesn't it</SMILE>

My motivation for posting is to provide a useful, efficient, clear, and
reasonable solution to the problem provided.


Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org