[31891] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3154 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 30 14:09:32 2010

Date: Thu, 30 Sep 2010 11:09:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 30 Sep 2010     Volume: 11 Number: 3154

Today's topics:
    Re: Claimed to be an "old hack" <nospam-abuse@ilyaz.org>
    Re: Long script "just stops" sometime <whynot@pozharski.name>
    Re: Need Regex for phone number <marc.girod@gmail.com>
    Re: Need Regex for phone number <RedGrittyBrick@spamweary.invalid>
    Re: Need Regex for phone number <marc.girod@gmail.com>
    Re: Need Regex for phone number <RedGrittyBrick@spamweary.invalid>
    Re: Need Regex for phone number <hjp-usenet2@hjp.at>
    Re: Need Regex for phone number <hjp-usenet2@hjp.at>
    Re: Need Regex for phone number <hjp-usenet2@hjp.at>
    Re: Need Regex for phone number <cwilbur@chromatico.net>
    Re: Need Regex for phone number <cwilbur@chromatico.net>
    Re: Need Regex for phone number <cwilbur@chromatico.net>
    Re: Need Regex for phone number <ralph@happydays.com>
    Re: Need Regex for phone number sln@netherlands.com
    Re: Need Regex for phone number <cartercc@gmail.com>
    Re: Need Regex for phone number <stanley@peak.org>
    Re: Time::Format::time with $SIG{__DIE__} problem <ben@morrow.me.uk>
    Re: Time::Format::time with $SIG{__DIE__} problem <nospam-abuse@ilyaz.org>
    Re: toy list processing problem: collect similar terms <namekuseijin@gmail.com>
    Re: toy list processing problem: collect similar terms <namekuseijin@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 30 Sep 2010 17:52:11 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Claimed to be an "old hack"
Message-Id: <slrnia9jib.luc.nospam-abuse@powdermilk.math.berkeley.edu>

On 2010-09-28, Ben Morrow <ben@morrow.me.uk> wrote:
>> >> >     (A && B) || C
>> >> >
>> >> > which is a nasty shorthand for
>> >> >
>> >> >     if (A) {
>> >> >         B
>> >> >     }
>> >> >     else {
>> >> >         C
>> >> >     }

>> > It's a very usual way of doing a compact if-then-else in
>> > shell, which is probably where it comes from.
>> 
>> Again, is s/way/wrong way/ assumed here?

> No, no value judgements were implied either way.

I did not mean "value judgement" here.  I meant semantic: your
"equivalence" holds only if B always succeeds if A succeeds.

Ilya


------------------------------

Date: Thu, 30 Sep 2010 09:34:20 +0300
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Long script "just stops" sometime
Message-Id: <slrnia8br9.qsk.whynot@orphan.zombinet>

with <4ca2b9dc$0$29845$ed362ca5@nr5-q3a.newsreader.com> Xho Jingleheimerschmidt wrote:
*SKIP*
> I don't have access to a more conventional Linux right now, but I
> don't recall seeing anything other than '-' and '?' listed under
> wchan.  I'll have to try to remember to give it a try.

Please note, observations could depend on distribution and/or the kernel
being self-built or of-stock.  I believe, kind of security-via-obscurity
idealism.  And that's not the first time I have to fight that in Debian,
in particular.

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


------------------------------

Date: Thu, 30 Sep 2010 01:06:40 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: Need Regex for phone number
Message-Id: <b5ee87dd-2805-44f2-acf6-962a98807039@x42g2000yqx.googlegroups.com>

On Sep 29, 9:55=A0pm, Charlton Wilbur <cwil...@chromatico.net> wrote:

> If you don't have basic Perl knowledge

But that's not what you measure, or determine, or show.
I repeat myself:
I tend to get rejected in such tests. It doesn't seem to depend on
the
area of expertise.

Do you mean that I am worth *nothing*?
Yes. I can see you do.

Marc


------------------------------

Date: Thu, 30 Sep 2010 10:43:31 +0100
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: Need Regex for phone number
Message-Id: <4ca45bc4$0$2512$db0fefd9@news.zen.co.uk>

On 29/09/2010 22:39, Tad McClellan wrote:
> Ted Zlatanov<tzz@lifelogs.com>  wrote:
>> "Do you play golf?"
> no

0

-- 
RGB


------------------------------

Date: Thu, 30 Sep 2010 03:23:11 -0700 (PDT)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: Need Regex for phone number
Message-Id: <ab891967-25a5-4035-839e-c3037067f45e@d17g2000yqm.googlegroups.com>

On Sep 30, 10:43=A0am, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:

> 0

Doesn't this sound paradoxical ?
I can see a circle in that 0...

Marc


------------------------------

Date: Thu, 30 Sep 2010 12:22:03 +0100
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: Need Regex for phone number
Message-Id: <4ca472dc$0$12156$fa0fcedb@news.zen.co.uk>

On 30/09/2010 11:23, Marc Girod wrote:
> On Sep 30, 10:43 am, RedGrittyBrick<RedGrittyBr...@spamweary.invalid>
> wrote:
>
>> 0
>
> Doesn't this sound paradoxical ?
> I can see a circle in that 0...
>

In multi-layered pleasure per byte, it probably exceeds anything I've 
written since first I applied crayon to wallpaper.

-- 
RGB


------------------------------

Date: Thu, 30 Sep 2010 16:30:37 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Need Regex for phone number
Message-Id: <slrnia97od.eg4.hjp-usenet2@hrunkner.hjp.at>

On 2010-09-29 08:01, Bart Lateur <bart.lateur@telenet.be> wrote:
> Charlton Wilbur wrote:
>>          We have a text-input field that we ask the user to type a
>>          telephone number into.  As part of data validation, we need to
>>          know if it's a valid phone number and to identify the area
>>          code.  Please write some code to do that.
>
> The only way to really be sure is to call up the number.

That still doesn't give you the area code. Although if somebody answers
the phone you could ask ...

	hp



------------------------------

Date: Thu, 30 Sep 2010 16:37:31 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Need Regex for phone number
Message-Id: <slrnia985b.eg4.hjp-usenet2@hrunkner.hjp.at>

On 2010-09-29 20:55, Charlton Wilbur <cwilbur@chromatico.net> wrote:
>>>>>> "MG" == Marc Girod <marc.girod@gmail.com> writes:
>    MG> On Sep 28, 6:05 pm, Charlton Wilbur <cwil...@chromatico.net> wrote:
>     >> You'd think that - but in practice, there are a lot of people who
>     >> present themselves as Perl experts convincingly enough to get
>     >> past HR people and generalist recruiters.
>
>    MG> This kind of interview strategy does select some people and
>    MG> reject others.
>
> That's exactly the point. 
>
>    MG> Whether the people selected are more suitable to the job than
>    MG> the people rejected is questionable. It is only obvious to
>    MG> certain kinds of people. It is not to others.
>
> If you don't have basic Perl knowledge -- and a level of regular
> expressions knowledge sufficient to make at least a reasonable attempt
> at solving that problem

I think I have both. But if you asked me that question you wouldn't get
a Perl answer. Instead I would ask you why you needed that information,
I would tell you about various conventions for writing telephone
numbers, maybe suggest a few heuristics, ask what should happen if those
heuristics fail, how they should be maintained, etc. Only if all of that
is clear (and that would take a lot more that the 5 minutes you
mentioned earlier) would I start to attempt and implementation, and I'm
not at all sure whether it would involve regexps.

	hp



------------------------------

Date: Thu, 30 Sep 2010 16:47:25 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Need Regex for phone number
Message-Id: <slrnia98nu.eg4.hjp-usenet2@hrunkner.hjp.at>

On 2010-09-29 21:09, Ted Zlatanov <tzz@lifelogs.com> wrote:
> On Tue, 28 Sep 2010 13:05:04 -0400 Charlton Wilbur <cwilbur@chromatico.net> wrote: 
> CW> You'd think that - but in practice, there are a lot of people who
> CW> present themselves as Perl experts convincingly enough to get past HR
> CW> people and generalist recruiters.
[...]
> "Do you play golf?"

Heh, I already "failed" that question once by answering "no, only
minigolf, and that rarely".
To my defense I can say that I was asked that question at a pub after a
few beers and without Perl context.

	hp



------------------------------

Date: Thu, 30 Sep 2010 11:18:37 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Need Regex for phone number
Message-Id: <86zkuz8dki.fsf@mithril.chromatico.net>

>>>>> "MG" == Marc Girod <marc.girod@gmail.com> writes:

    MG> But that's not what you measure, or determine, or show.  I
    MG> repeat myself: I tend to get rejected in such tests. It doesn't
    MG> seem to depend on the area of expertise.

    MG> Do you mean that I am worth *nothing*?  Yes. I can see you do.

No.  I mean that if you can't come up with a rough solution -- not a
perfect solution, not the solution we're looking for, but a rough
solution -- to that problem in five to ten minutes, there's a good
chance you're lacking in the sort of skills that this group uses every
day.  

Charlton





-- 
Charlton Wilbur
cwilbur@chromatico.net


------------------------------

Date: Thu, 30 Sep 2010 11:29:01 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Need Regex for phone number
Message-Id: <86r5gb8d36.fsf@mithril.chromatico.net>

>>>>> "BL" == Bart Lateur <bart.lateur@telenet.be> writes:

    BL> Charlton Wilbur wrote:

    >> We have a text-input field that we ask the user to type a
    >> telephone number into.  As part of data validation, we need to
    >> know if it's a valid phone number and to identify the area code.
    >> Please write some code to do that.

    BL> The only way to really be sure is to call up the number.

    BL> Yes, it is indeed similar to the problem of checking if an email
    BL> address is valid.

So substitute "well-formed" for "valid" in the spec.  Sheesh.

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


------------------------------

Date: Thu, 30 Sep 2010 11:27:05 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Need Regex for phone number
Message-Id: <86vd5n8d6e.fsf@mithril.chromatico.net>

>>>>> "PJH" == Peter J Holzer <hjp-usenet2@hjp.at> writes:

    PJH> I think I have both. But if you asked me that question you
    PJH> wouldn't get a Perl answer. Instead I would ask you why you
    PJH> needed that information, I would tell you about various
    PJH> conventions for writing telephone numbers, maybe suggest a few
    PJH> heuristics, ask what should happen if those heuristics fail,
    PJH> how they should be maintained, etc. Only if all of that is
    PJH> clear (and that would take a lot more that the 5 minutes you
    PJH> mentioned earlier) would I start to attempt and implementation,
    PJH> and I'm not at all sure whether it would involve regexps.

The answers: We need to make sure we have a reasonable phone number in
order to contact customers.  Marketing wants area code information
because it's the key into their demographics information system.  We're
only dealing with North American phone numbers here, and we're using
freeform form field entry, so we're dealing with whatever people give
us.  If validation fails, we just respond with a "Your phone number was
not understood" message, and ask the user to re-enter it. 

And at some point in there I'd congratulate you on your thoroughness and
remind you that my role in the interview process is largely to assess
your experience and skill with Perl, not your adherence to good software
engineering practices or your arcane knowledge of telephone number formats.

Seriously: the point of the question is not to determine who has worth
as a human being or to solve the problem once and for all time.  The
point of the question is to ask a question that's reasonably well-scoped
but with a lot of fuzzy areas and possibilities for human error, to see
how the candidate approaches the problem.  You're not supposed to get
the right answer, because there is no right answer.  

Charlton






-- 
Charlton Wilbur
cwilbur@chromatico.net


------------------------------

Date: Thu, 30 Sep 2010 11:35:41 -0400
From: Ralph Malph <ralph@happydays.com>
Subject: Re: Need Regex for phone number
Message-Id: <cc8e7$4ca4ae4e$ce534406$19660@news.eurofeeds.com>

On 9/23/2010 5:05 PM, Tad McClellan wrote:
> lotug<ernesto@ernestoreyes.com>  wrote:
>
>> I need Regex to indentify 3108222400 phone number.
>
> No you don't, a simple equality test will suffice for that:
>
>      $phone_number eq '3108222400';
>
>
> The quality of the answers you receive here is in direct
> proportion to the quality of the questions you post here...
>
> Have you seen the Posting Guidelines that are posted here frequently?
ha ha ha ha, of course he has *seen* them.
Since it is a lengthy piece of unreadable excrement *seeing* them
doesn't seem to much matter, now does it?
Told you so.



------------------------------

Date: Thu, 30 Sep 2010 08:54:15 -0700
From: sln@netherlands.com
Subject: Re: Need Regex for phone number
Message-Id: <m3b9a6ll6iombp3n53th8kgg2e8bbge6d8@4ax.com>

On Thu, 30 Sep 2010 11:18:37 -0400, Charlton Wilbur <cwilbur@chromatico.net> wrote:

>>>>>> "MG" == Marc Girod <marc.girod@gmail.com> writes:
>
>    MG> But that's not what you measure, or determine, or show.  I
>    MG> repeat myself: I tend to get rejected in such tests. It doesn't
>    MG> seem to depend on the area of expertise.
>
>    MG> Do you mean that I am worth *nothing*?  Yes. I can see you do.
>
>No.  I mean that if you can't come up with a rough solution -- not a
>perfect solution, not the solution we're looking for, but a rough
>solution -- to that problem in five to ten minutes, there's a good
>chance you're lacking in the sort of skills that this group uses every
>day.  
>
A phone number rough solution is like it has 4 wheels when
designing a mazarati.

One solution/one type, group of edit boxes, pre-defined,
restricted to digit chars, validated with onselchange or
lose focus notify.

Parsing a single text entry for all the possible forms of a
phone number is quite another thing. This is usually what is
seen in on-line user entry forms. This way usually requires user
validation, or a forced re-entry, to conform to specific
pre-determined forms, or both. Then there are credit card numbers ..

-sln


------------------------------

Date: Thu, 30 Sep 2010 10:18:39 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Need Regex for phone number
Message-Id: <5c1b6be5-539e-4cf9-9f93-1f53b015530d@c10g2000yqh.googlegroups.com>

On Sep 28, 12:13=A0pm, Charlton Wilbur <cwil...@chromatico.net> wrote:
> =A0 =A0 =A0 =A0 =A0 We have a text-input field that we ask the user to ty=
pe a
> =A0 =A0 =A0 =A0 =A0 telephone number into. =A0As part of data validation,=
 we need to
> =A0 =A0 =A0 =A0 =A0 know if it's a valid phone number and to identify the=
 area
> =A0 =A0 =A0 =A0 =A0 code. =A0Please write some code to do that.

A North American telephone number consists of a three digit area code
(NPA), a three digit exchange (NXX), and a four digit number,
optionally preceded or followed by other digits. As long as you get
ten digits, you don't need a regex to separate the NPA, NXX, and
number -- substr will do just as well.

I just came through a major project like this, and the core of the
solution was to use a NPA/NXX database for a lookup. My project
required mapping numbers to geographical areas (using zipcodes) and
proceeded from the fallacious assumption that the zip code assigned to
the telephone billing center indicates the area of the subscriber's
residence (it doesn't).

My data doesn't come through user input, but from a call center which
records the number of the incoming call. The application processes
several thousand calls a day, and we have a significant number of
'numbers' that can't be mapped. Those numbers are (mostly) formatted
properly, but are not valid North American numbers.

Assuming that you collect your data via an HTML form, the JavaScript
form validation routines do a fairly good job of validating format,
depending on the specification for the input of data, e.g., requiring
input like '999-999-9999'. Assuming that you can retrieve the first
ten digits, optionally preceded by a '1', it's trivial to validate the
input as a 'telephone number.' However, to map the input to an area
code and exchange, your program should test the input against the
database.

The two biggest challenges are (1) dealing with the execution speed of
the script because of the amount of the data, and (2) fighting the
assumption that the data is valid. I solved the first challenge by
collecting all the numbers and running the script at 3:00 a.m. daily.
I haven't solved the second problem, and have concluded that it's
insoluble.

CC.


------------------------------

Date: Thu, 30 Sep 2010 10:48:02 -0700
From: John Stanley <stanley@peak.org>
Subject: Re: Need Regex for phone number
Message-Id: <alpine.LRH.2.00.1009301025190.2338@shell.peak.org>

On Thu, 30 Sep 2010, Charlton Wilbur wrote:

> No.  I mean that if you can't come up with a rough solution -- not a
> perfect solution, not the solution we're looking for, but a rough
> solution -- to that problem in five to ten minutes,

Ten seconds. /([2-9]\d\d)/. But that requires an "esoteric knowledge" 
of the NANP, that area codes don't start with 0 or 1, and that they 
changed to allow 2-9 as a middle digit. Do I win?

> there's a good
> chance you're lacking in the sort of skills that this group uses every
> day.

From your other posts on this, here is what I come up with as the "skills" 
you are looking for:

>So substitute "well-formed" for "valid" in the spec.  Sheesh.

1. Programmers who will make substantive changes to a specification to 
simplify the problem for themselves.

>The answers: We need to make sure we have a reasonable phone number in
>order to contact customers.

2. A willingness to "do what one it told" despite ethical questions, 
similar to the skillset of an entry-level telemarketer.

> We're only dealing with North American phone numbers here,

3. Active support for ethno-centrism and a willingness to limit company 
growth into new markets. Additionally, a willingness to encode into legacy 
software limits that may well start insulting your new "foreign" customers 
when you do eventually expand and your web page tells them their "country 
code plus a few digits of their city code" is just an "area code", as if 
everywhere is just an "area" of the US.

> and we're using freeform form field entry,

4. A willingness to apply the wrong tool at the wrong place in the data 
chain to make up for a lack of programming skills on the part of the web 
page providers.

> If validation fails, we just respond with a "Your phone number was
> not understood" message, and ask the user to re-enter it.

5. An attitude of superiority that has the backend perl programmer telling 
people they don't know what their phone number (or email address is, for 
those that put artificial limits on what a "valid email address" can look 
like and reject anything they don't understand) really is or how it 
should properly be expressed.

Do I have that about right?




------------------------------

Date: Thu, 30 Sep 2010 00:14:02 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Time::Format::time with $SIG{__DIE__} problem
Message-Id: <qqtcn7-7jo2.ln1@osiris.mauzo.dyndns.org>


Quoth Mark <google@markginsburg.com>:
> On Sep 29, 12:33 pm, s...@netherlands.com wrote:
> > >I am thinking the following might be better:
> >
> > >return() unless defined $^S and $^S == 0; # ignore unless program exit
> >
> > unless defined $^S and $^S == 0 will will be true if:
> >  $^S < 0 || undef || $^S > 0
> >
> > if $^S will will be true if:
> >  $^S < 0 || $^S > 0
> >
> > Is that what you want?
> 
> I don't think so, but I'm in a bit over my head here.
> 
> According to perlvar:
> 
>   $^S         State
>   ---------   -------------------
>   undef       Parsing module/eval
>   true (1)    Executing an eval
>   false (0)   Otherwise
> 
> So I think I should return immediately for cases where $^S is undef as
> well as equal to 1 as I only want to deal with abnormal code
> termination, for instance due to a call to die.
> 
> "perldoc -f die" suggests putting "die @_ if $^S;" as the first line
> of the sig die handler. But that wouldn't
> handle the case where the handler was called when "parsing module/
> eval".  I have never seen this happen but I
> feel that handling this case as well would be prudent.

The standard idiom is

    die @_ if $^S or not defined $^S;

I *swear* this is (or used to be) in the standard docs somewhere, but I
can't find it now.

Ben




------------------------------

Date: Thu, 30 Sep 2010 18:06:20 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Time::Format::time with $SIG{__DIE__} problem
Message-Id: <slrnia9kcs.luc.nospam-abuse@powdermilk.math.berkeley.edu>

On 2010-09-29, Mark <google@markginsburg.com> wrote:
>   $^S         State
>   ---------   -------------------
>   undef       Parsing module/eval
>   true (1)    Executing an eval
>   false (0)   Otherwise
>
> So I think I should return immediately for cases where $^S is undef as
> well as equal to 1 as I only want to deal with abnormal code
> termination, for instance due to a call to die.
>
> "perldoc -f die" suggests putting "die @_ if $^S;" as the first line
> of the sig die handler. But that wouldn't
> handle the case where the handler was called when "parsing module/
> eval".  I have never seen this happen but I
> feel that handling this case as well would be prudent.

The usual way to report parse errors is to call __WARN__, then
continue parsing (to report as many parse errors as possible), then
report a final parse error.

perl -wle "sub v {defined $^S ? $^S : q(undef)};
	   $SIG{__WARN__} = sub {my $v = v; print qq(in WARN($v): @_\n)};
	   $SIG{__DIE__}  = sub {my $v = v; print qq(in DIE($v): @_\n)};
	   eval q(s//)"
in DIE(undef): Substitution replacement not terminated at (eval 1) line 1.

Ilya


------------------------------

Date: Thu, 30 Sep 2010 05:35:20 -0700 (PDT)
From: namekuseijin <namekuseijin@gmail.com>
Subject: Re: toy list processing problem: collect similar terms
Message-Id: <6784f34c-2948-408b-b2eb-4a3b171c5afe@k10g2000yqa.googlegroups.com>

On 29 set, 11:04, w_a_x_man <w_a_x_...@yahoo.com> wrote:
> On Sep 26, 9:24=A0am, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>
>
>
> > Xah Lee <xah...@gmail.com> writes:
> > > here's a interesting toy list processing problem.
>
> > > I have a list of lists, where each sublist is labelled by
> > > a number. I need to collect together the contents of all sublists
> > > sharing
> > > the same label. So if I have the list
>
> > > ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> > > r) (5 s t))
>
> > > where the first element of each sublist is the label, I need to
> > > produce:
>
> > > output:
> > > ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
> > > a Mathematica solution is here:
> > >http://xahlee.org/UnixResource_dir/writ/notations_mma.html
>
> > > R5RS Scheme lisp solution:
> > >http://xahlee.org/UnixResource_dir/writ/Sourav_Mukherjee_sourav.work_.=
 ..
> > > by Sourav Mukherjee
>
> > > also, a Common Lisp solution can be found here:
> > >http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1de.=
 ..
>
> > It's too complex. Just write:
>
> > (let ((list '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 (2 o p) (4 q r) (5 s t))))
>
> > =A0 (mapcar (lambda (class) (reduce (function append) class :key (funct=
ion rest)))
> > =A0 =A0 =A0 =A0 =A0 =A0(com.informatimago.common-lisp.list:equivalence-=
classes list :key (function first)))
>
> > =A0 =A0)
>
> > --> ((S T) (Q R M N) (G H) (O P K L E F) (I J C D) (A B))
>
> > --
> > __Pascal Bourguignon__ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0http://ww=
w.informatimago.com/
>
> Ruby:
>
> [[0, 'a', 'b'], [1, 'c', 'd'], [2, 'e', 'f'], [3, 'g', 'h'], [1,
> 'i', 'j'], [2, 'k', 'l'], [4, 'm', 'n'], [2, 'o', 'p'], [4, 'q', 'r'],
> [5, 's', 't']].
> group_by{|x| x.first}.values.map{|x| x.map{|y| y[1..-1]}.flatten}
>
> =A0 =A0 =3D=3D>[["s", "t"], ["a", "b"], ["c", "d", "i", "j"],
> =A0["e", "f", "k", "l", "o", "p"],
> =A0["g", "h"], ["m", "n", "q", "r"]]

cool, it comes with order all fucked up.  This is something I was
criticized for before, though not all that important to most
functional processing.  Not the case here, though.

here's a scheme version that is hopefully better than the given one:

(define (dig in)

  (if (null? in) '()

    (let* ((n         (first-of-first in))

           (all-n     (filter in (lambda (x)      (eq? n (first x)))))

           (all-but-n (filter in (lambda (x) (not (eq? n (first
x)))))))

       (pair

          (fold all-n
             (lambda (i o) (pair (second i) (pair (third i) o))))

          (dig all-but-n)))))


; given these aliases to lisp n00bs

(define pair cons)

(define first car)

(define rest cdr)

(define first-of-first caar)

(define second cadr)

(define third caddr)


; and these well-known functions
 (non-tail-recursive for benefit of n00bs)
(define (fold ls f) ; AKA reduce

  (if (null? ls) '()

      (f (first ls) (fold (rest ls) f))))


(define (filter ls f)

  (fold ls (lambda (i o) (if (f i) (pair i o) o))))



; testing
(let ((in '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)
            (2 o p) (4 q r) (5 s t))))
  (display (dig in))
  (newline))



------------------------------

Date: Thu, 30 Sep 2010 06:32:32 -0700 (PDT)
From: namekuseijin <namekuseijin@gmail.com>
Subject: Re: toy list processing problem: collect similar terms
Message-Id: <fd1070df-db28-4c19-a8d3-95e57824f836@w19g2000yqb.googlegroups.com>

On 30 set, 09:35, namekuseijin <namekusei...@gmail.com> wrote:
> On 29 set, 11:04, w_a_x_man <w_a_x_...@yahoo.com> wrote:
>
>
>
> > On Sep 26, 9:24=A0am, p...@informatimago.com (Pascal J. Bourguignon)
> > wrote:
>
> > > Xah Lee <xah...@gmail.com> writes:
> > > > here's a interesting toy list processing problem.
>
> > > > I have a list of lists, where each sublist is labelled by
> > > > a number. I need to collect together the contents of all sublists
> > > > sharing
> > > > the same label. So if I have the list
>
> > > > ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4=
 q
> > > > r) (5 s t))
>
> > > > where the first element of each sublist is the label, I need to
> > > > produce:
>
> > > > output:
> > > > ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
> > > > a Mathematica solution is here:
> > > >http://xahlee.org/UnixResource_dir/writ/notations_mma.html
>
> > > > R5RS Scheme lisp solution:
> > > >http://xahlee.org/UnixResource_dir/writ/Sourav_Mukherjee_sourav.work=
_...
> > > > by Sourav Mukherjee
>
> > > > also, a Common Lisp solution can be found here:
> > > >http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1d=
e...
>
> > > It's too complex. Just write:
>
> > > (let ((list '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 (2 o p) (4 q r) (5 s t))))
>
> > > =A0 (mapcar (lambda (class) (reduce (function append) class :key (fun=
ction rest)))
> > > =A0 =A0 =A0 =A0 =A0 =A0(com.informatimago.common-lisp.list:equivalenc=
e-classes list :key (function first)))
>
> > > =A0 =A0)
>
> > > --> ((S T) (Q R M N) (G H) (O P K L E F) (I J C D) (A B))
>
> > > --
> > > __Pascal Bourguignon__ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0http://=
www.informatimago.com/
>
> > Ruby:
>
> > [[0, 'a', 'b'], [1, 'c', 'd'], [2, 'e', 'f'], [3, 'g', 'h'], [1,
> > 'i', 'j'], [2, 'k', 'l'], [4, 'm', 'n'], [2, 'o', 'p'], [4, 'q', 'r'],
> > [5, 's', 't']].
> > group_by{|x| x.first}.values.map{|x| x.map{|y| y[1..-1]}.flatten}
>
> > =A0 =A0 =3D=3D>[["s", "t"], ["a", "b"], ["c", "d", "i", "j"],
> > =A0["e", "f", "k", "l", "o", "p"],
> > =A0["g", "h"], ["m", "n", "q", "r"]]
>
> cool, it comes with order all fucked up. =A0This is something I was
> criticized for before, though not all that important to most
> functional processing. =A0Not the case here, though.
>
> here's a scheme version that is hopefully better than the given one:

(define (dig in)
=A0 (if (null? in) '()
=A0 =A0 (let* ((n =A0 =A0 =A0 =A0 (first-of-first in))
=A0 =A0 =A0 =A0 =A0 =A0(all-n =A0 =A0 (filter in (lambda (x) =A0 =A0 =A0(eq=
? n (first x)))))
=A0 =A0 =A0 =A0 =A0 =A0(all-but-n (filter in (lambda (x) (not (eq? n (first
x)))))))
=A0 =A0 =A0 =A0(pair
=A0 =A0 =A0 =A0 =A0 (fold all-n
=A0 =A0 =A0 =A0 =A0 =A0 =A0(lambda (i o) (pair (second i) (pair (third i) o=
))))
=A0 =A0 =A0 =A0 =A0 (dig all-but-n)))))

; given these aliases to lisp n00bs
(define pair cons)
(define first car)
(define rest cdr)
(define first-of-first caar)
(define second cadr)
(define third caddr)

; and these well-known functions=A0(non-tail-recursive for benefit of
n00bs)
(define (fold ls f) ; AKA reduce
=A0 (if (null? ls) '()
=A0 =A0 =A0 (f (first ls) (fold (rest ls) f))))
(define (filter ls f)
=A0 (fold ls (lambda (i o) (if (f i) (pair i o) o))))

; testing
(let ((in '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)
=A0 =A0 =A0 =A0 =A0 =A0 (2 o p) (4 q r) (5 s t))))
=A0 (display (dig in))
=A0 (newline))

;frakkin text editor...


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3154
***************************************


home help back first fref pref prev next nref lref last post