|
Prev: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.8 $)
Next: FAQ 1.13 Is it a Perl program or a Perl script?
From: nolo contendere on 23 Apr 2008 10:46 On Apr 22, 11:49 pm, benkasminbull...(a)gmail.com wrote: > nolo contendere <simon.c...(a)fmr.com> wrote: > > I'm not sure how much control you have over the structure of the data, > > but it seems to me that you would be better served if the data were > > stored in hashes to begin with: > > If the person needs to know how to put in a screw, why tell him he > would be better served by using nails and a hammer? huh? i began my suggestion with the caveat that this would be relevant if the OP had the control to choose the data structures in which he housed his data. to follow up on your analogy, if the person needs to know how to build a house, and is scratching his head wondering how to do it with only screws and a screwdriver, yes, i could help him by telling him how to go about it. or, i can point out that there is a tool shed around the corner of which he is unaware, and then proceed to pick out more appropriate tools that he can use to better build his house. here's another analogy. if he asked, how do i add the following? <<END_WORD_PROBLEM; i want to add three 6's and six 4's using addition. sure, you could say: well, first compute the sum of the three 6's, like so-- 6 + 6 + 6 = 18. then, compute the sum of six 4's, like so-- 4 + 4 + 4 + 4 + 4 + 4 = 24. finally, add the two sums together. 18 + 24 = 42. END_WORD_PROBLEM Or, I could make life a whole lot easier by suggesting: just do: (3 x 6) + (6 x 4) = 42. what's wrong with that? at the very worst, if there are restrictions in place that prohibit him from using multiplication for this particular problem, he is at least aware that there is a better solution for similar problems in the future, when such restrictions may may be lifted, or not even exist at all. ...or did you just want an excuse to post that quote by Steven King? ;-).
From: Ben Bullock on 23 Apr 2008 18:47 On Wed, 23 Apr 2008 07:46:35 -0700, nolo contendere wrote: > On Apr 22, 11:49 pm, benkasminbull...(a)gmail.com wrote: >> If the person needs to know how to put in a screw, why tell him he >> would be better served by using nails and a hammer? > > > huh? i began my suggestion with the caveat that this would be relevant > if the OP had the control to choose the data structures in which he > housed his data. to follow up on your analogy, if the person needs to > know how to build a house, and is scratching his head wondering how to > do it with only screws and a screwdriver, yes, i could help him by > telling him how to go about it. Well, I'm sorry to offend you. > ...or did you just want an excuse to post that quote by Steven King? > ;-). No, I think it was the correct time to post that quote. Or maybe a more relevant quote would be something about how if you endlessly try to finesse things, you'll never actually get the job done. Perl programmers should know how to deal with awkward data formats like the original poster's, because these do occur in practice, and they may be unavoidable for one reason or another.
From: Tad J McClellan on 23 Apr 2008 23:02 benkasminbullock(a)gmail.com <benkasminbullock(a)gmail.com> wrote: > nolo contendere <simon.chao(a)fmr.com> wrote: > >> I'm not sure how much control you have over the structure of the data, >> but it seems to me that you would be better served if the data were >> stored in hashes to begin with: > > If the person needs to know how to put in a screw, why tell him he > would be better served by using nails and a hammer? Errr, because he would be better served, of course. Q: I need to put a lot of screws in to hold the shingles on the roof. Can anyone suggest a good power screwdriver? A: You would be better served by using nails and a nail gun. If you use screws it will take you 2 days to finish instead of 1 day. Even worse, when you need to maintain your roof, it will take you 4 days to remove the old one instead of 1 day if you had used nails. Choice of an approriate data structure is very important in designing a software solution. (It made this task into a one-liner.) If you don't have a choice of data structure, then you must bite the bullet and write 10 or 20 lines of code. nolo gave an appropriate predicate to his advice. Seems clear enough that the advice should be skipped if the predicate is not true... -- Tad McClellan email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
From: Ben Bullock on 23 Apr 2008 23:16
Tad J McClellan <tadmc(a)seesig.invalid> wrote: >> If the person needs to know how to put in a screw, why tell him he >> would be better served by using nails and a hammer? > > > Errr, because he would be better served, of course. > > > Q: I need to put a lot of screws in to hold the shingles on the roof. > Can anyone suggest a good power screwdriver? > > A: You would be better served by using nails and a nail gun. Since I have no idea what the original poster's task was, why speculate about it? > Choice of an approriate data structure is very important in designing > a software solution. (It made this task into a one-liner.) > > If you don't have a choice of data structure, then you must > bite the bullet and write 10 or 20 lines of code. My original solution had ten lines of code, it's true. But if you want to reduce the lines of code, it's perfectly possible: my %ak = (map{$$_[0],$_} @a); my %bk = (map{$$_[0],$_} @b); for (sort keys %ak) {my $v=$bk{$_}; push @c, [@{$ak{$_}}, @$v[1..$#$v]] if $v} unshift @c, pop @c; # Just for Harmut Camphausen That has the disadvantage of being less clear to read but it does exactly the same thing. If I was doing this for real I'd be much more worried about problems such as empty values in the first column of the array, or duplicate IDs in @a, or what to do with values which are in @a but not in @b or vice-versa, than I would be about turning the whole thing into a hash keyed by the values on the first line. That could just as easily make other parts of the program more complicated, and it seems to me that Perl programmers should be able to write code to deal with arrays of arrays without getting confused, or write a regular expression to match single quoted strings. So I'm not sure what the point of the original advice was. To me it's just confusing the issue. |