Subject: RE: [hackers-il] You know you've been hacking Perl too long when. ..

From: Chen Shapira <[email-masked]>
Date: Mon, 4 Mar 2002 10:26:02 +0200

> my @result = sort { $_->{'state'} cmp $_->{'state'} } @input;

Since I barely know perl, let me try to decypher it. Let me know how wrong I
am...

1. I'm not sure what "my" is. I think it means a local variable? anyway, I
assume its a qualifier.
2. @result - an array of results.
which makes...
3. @input an array of inputs (at this point I wish you used hungarian so I'd
know what this array holds...)
4. sort {...} - my guess is that you are defining a function and applying it
on @input?
5. I suppose cmp is some sort of an operator?
6. Although it looks like you are comparing the same thing twice, I'd guess
that looking at {'state'} changes it in some way, otherwise it makes no
sense.
7. Acutally it makes no sense anyway! I'm quite sure you are sorting input
into results, but where are you sorting it???

*sigh*

How silly did I sound?

---

From: "Nadav Har'El" <[email-masked]>
Date: Mon, 4 Mar 2002 11:32:13 +0200

On Mon, Mar 04, 2002, Chen Shapira wrote about "RE: [hackers-il] You know you've been hacking Perl too long when. ..":
>...

I know Perl quite well, and it still doesn't make much sense to me... The
{...} block should be a block that defines how to compare a pair of @input
elements, and should use the variables $a and $b! See the perlfunc manual
for a working example:

                   # sort lexically
                   @articles = sort @files;

                   # same thing, but with explicit sort routine
                   @articles = sort {$a cmp $b} @files;

Since we have no idea what you expected $_ to contain, or whether it is
a "state" key (or maybe $_ is an object and state is a function, but how
can that be???), or why you're comparing something with itself, obviously
we have no idea what you're talking about...

-- 
Nadav Har'El                        |         Monday, Mar 4 2002, 20 Adar 5762
[email-masked]             |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |Today is the tomorrow you worried about
http://nadav.harel.org.il           |yesterday, and now you know why.

---

From: Shlomi Fish <[email-masked]>
Date: Mon, 4 Mar 2002 13:41:33 +0200 (IST)

> we have no idea what you're talking about...

The point was that I used $_ twice instead of using $a and $b. I'm too
used to map, grep and friends which accept $_ as their iteration
experssion, so I applied the same knowledge to sort, not remembering it
was $a and $b instead.

But a joke is not very funny once you explain it.

Regards,

	Shlomi Fish

> To unsubscribe from this group, send an email to:
> [email-masked]
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>

----------------------------------------------------------------------
Shlomi Fish        [email-masked]
Home Page:         http://t2.technion.ac.il/~shlomif/
Home E-mail:       [email-masked]

"Let's suppose you have a table with 2^n cups..."
"Wait a second - is n a natural number?"

---

From: Shlomi Fish <[email-masked]>
Date: Mon, 4 Mar 2002 14:07:18 +0200 (IST)

> 1. I'm not sure what "my" is. I think it means a local variable? anyway, I

my indeed declares a local variable.

> 2. @result - an array of results.

Correct.

> know what this array holds...)

In Perl it doesn't matter. But for this particular cause it holds an array
of structs (implemented as hashes) which contain a member called 'state'.

> 4. sort {...} - my guess is that you are defining a function and applying it

Wrong. sort is a pre-built function that simply accepts some kind of block
construct. It applies the experssion inside the block repetitively based
on the input of the text.

map and grep behave the same, but I'm not aware of any user-land functions
that can be written this way in Perl 5. That's one of the things I'd like
to fix in Rindolf.

> 5. I suppose cmp is some sort of an operator?

cmp compares two strings. $a cmp $b returns a negative number if $a is
lexically lower than $b, zero if they are the same string, and a positive
number if they $a is lexically higher than $b. The operator "<=>" does the
same for numbers. Both are very convenient for sorting and the such.

You can also cascade them: ($a->{'state'} <=> $b->{'state'}) &&
($a->{'id'} <=> $b->{'id'}). Is it cool or what?

> that looking at {'state'} changes it in some way, otherwise it makes no

Actually, it doesn't change it at all. Like I said in the answer to Nadav,
I used $_ instead of $a and $b by mistake.

> into results, but where are you sorting it???

I am not. This snippet will sort the records based on some arbitrary
order, or simply not sort them at all.

> How silly did I sound?

YHBT, YHL, HAND.

Actually, I did not mean to troll you, but it just came out this way. If
you find Perl useful at times, it would be worth your time to dedicate
some spare moments to learn it better. (some people read perlfunc for
fun). If you don't like perl, switch to python or Ruby or whatever.

And no - I don't consider ASP or JSP as truly near them. Maybe I made a
mistake by automatically assumed that many people here know Perl well
enough. What languages constitute the lowest common denominator of
Hackers-IL (or at least its core)?

* ANSI C - Almost %100.
* C++ - 80-90%
* Perl - OmerM does not know it well, and maybe Chen does not either.
* Scheme - someone commented on my "Long Live the Minute Difference" that
he did not understand it.
* Haskell - There's at least one person here who knows it.
* Java - Below %50.
* Common Lisp - Oleg, OmerM, and then some.
* Awk - Nadav, me, Oleg. Anybody else bothered learning it?
* Bourne Shell - Me, Nadav, Tzafrir, Chen (?), OmerM, Oleg. Muli and Guy
do not seem to know it.
* C Shell - Muli, Guy, and some of the Bourne Shell people, who on their
part try to avoid it like the plague.
* Fortran - Nadav, OmerZ, Oleg. Anybody else?

Naturally, I'm not going to list all computer languages here. But the
conclusion is clear: we should use strictly ANSI C from now on.

Regards,

	Shlomi Fish

There is no IGLU Cabal! It took a 100*10^6 lines of code to write its core
in ANSI C, due to the fact it was the lowest common denominator among its
members.

---

From: Omer Zak <[email-masked]>
Date: Mon, 4 Mar 2002 14:38:52 +0200 (EET)

> Hackers-IL (or at least its core)?

Irrelevant.

I follow (and write to) the Hackers-IL mailing list in order to learn new
things, expand my horizons and have fun.
I have no problems with people discussing interesting ideas using
unfamiliar languages.  So far, there was no problem asking those people to
explain whatever I needed to fill the holes in my knowledge of whatever I
needed to understand the point.

> conclusion is clear: we should use strictly ANSI C from now on.

I don't want to see Hackers-IL degenerate to the lowest common denominator
of its participants.  The lowest which I'd accept is their LCM (lowest
common multiplier).

> in ANSI C, due to the fact it was the lowest common denominator among its

                                             --- Omer
There is no IGLU Cabal.  The lowest common denominator of the relevant
people turned out to be strictly negative.
WARNING TO SPAMMERS:  at http://www.zak.co.il/spamwarning.html

---

From: Shlomi Fish <[email-masked]>
Date: Mon, 4 Mar 2002 14:58:58 +0200 (IST)

Terribly sorry for the confusion. I seem to confuse between opposites
quite a lot. I naturally meant the _greatest_ common divisor.

Sorry everybody.

Regards,

	Shlomi Fish

---

From: "Nadav Har'El" <[email-masked]>
Date: Mon, 4 Mar 2002 16:58:15 +0200

> ($a->{'id'} <=> $b->{'id'}). Is it cool or what?

Nice :) Of course it assumes a particular definition of (x && y), which is
(x ? x : y), i.e., if x is non-zero return x (not 1!), otherwise return y.

Is this the defintion used in Perl? I have to admit I never checked.

This (x ? x : y) (where x,y are the results of the two comparisons above)
gives you lexical order lexical order because you first make comparison x;
if they are not equal the result is the result of that comparison. otherwise, 
you make the comparison y and use that result.

-- 
Nadav Har'El                        |         Monday, Mar 4 2002, 20 Adar 5762
[email-masked]             |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |Unix is simple, but it takes a genius to
http://nadav.harel.org.il           |understand its simplicity.

---

From: "Nadav Har'El" <[email-masked]>
Date: Mon, 4 Mar 2002 17:37:59 +0200

> Is this the defintion used in Perl? I have to admit I never checked.

I just checked, and this is true, but of course with ||, not && :)
You meant ||, right?

-- 
Nadav Har'El                        |         Monday, Mar 4 2002, 20 Adar 5762
[email-masked]             |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |I don't eat snails. I prefer fast food.
http://nadav.harel.org.il           |

---

From: Adi Stav <[email-masked]>
Date: Mon, 4 Mar 2002 19:40:39 +0200

> (x ? x : y), i.e., if x is non-zero return x (not 1!), otherwise return y.

That's one of the nicest things in Perl, although I'm not sure if 
Perl invented it. I wish there was something similar in C -- it 
could have fit the language /very/ nicely. Run faster without 
optimizations, too. Too bad the standard guarantees 1 rather than
any non-zero.

---

From: Shlomi Fish <[email-masked]>
Date: Mon, 4 Mar 2002 18:16:01 +0200 (IST)

> optimizations, too. Too bad the standard guarantees 1 rather than

I think you can hack a cmp macro in C:

#define cmp(a,b) (((a)<(b))?(-1):((a)>(b)?1:0)))

I wrote it like this so it may need a few more parenthesiss. Naturally,
writing a macro like this can croak on you in so many ways. You can use an
inline function or a gcc statement expression, but they are not as
portable. (despite the fact that "inline" is in the C99 standard, not all
compiler implemented it yet)

Regards,

	Shlomi Fish

---

From: Shlomi Fish <[email-masked]>
Date: Mon, 4 Mar 2002 18:09:38 +0200 (IST)

> I just checked, and this is true, but of course with ||, not && :)

I think I meant &&, but I always confuse between the two when used this
way.

Regards,

	Shlomi Fish

Knuth is not God! Unless you confuse him with Dijkstra.

---

From: mulix <[email-masked]>
Date: Mon, 4 Mar 2002 19:39:35 +0200

> optimizations, too. Too bad the standard guarantees 1 rather than

are you sure? a small test shows that the expression (x ? x : y)
evaluates to x if x is true, and y otherwise. or did i misunderstand?

ObKernel: ever seen (x ?: y) ? how about (!!x) ?
-- 
The ill-formed Orange		
Fails to satisfy the eye:       http://vipe.technion.ac.il/~mulix/ 
Segmentation fault.		http://syscalltrack.sf.net/

---

From: Adi Stav <[email-masked]>
Date: Thu, 7 Mar 2002 14:32:02 +0200

> evaluates to x if x is true, and y otherwise. or did i misunderstand?

( x ? x : y) is not the same as Perl's (x || y) because x is typed
twice and evaluated twice. Perl's || could have been very elegant and
convenient in long C expressions, as x ||= y could have been, without 
using temporary variables. Of course, you can simulate any operator 
in assembly if you're willing to type enough and don't care for how 
it looks :) 

> ObKernel: ever seen (x ?: y) ? 

Hmm, apparently this acts like Perl' || in GCC. Not real C, though.

> how about (!!x) ?

I use it at times. Works in K&R and all.