Re: limit the list

2002-11-20 Thread Chris Dolan
A. Pagaltzis wrote:
> * Dave Arnold <[EMAIL PROTECTED]> [2002-11-21 02:45]:
> 
>>$str = join ', ', @names;
>>if ( length($str) > 90 )
>>{
>>  substr($str,rindex($str,",",90)) = ", etc.";
>>}
> 
> 
> Nice, and so obvious too - in retrospect, of course.
> 
> The only thing I don't like about it is it still joins
> everything, even if it only needs 3 out of 10,000 elements.
> 

How about a simple change of the first line to:

  $str = join ', ', @names[0..30];

With a "," and a " ", each element must have at least 3 characters
(ignoring the posibility of empty elements), so there will be at most 30
entries in the final string.  I include the 31st to trigger the
", etc.".

[Others have presented very similar ideas]

Chris




Re: limit the list

2002-11-20 Thread A. Pagaltzis
* Jonathan E. Paton <[EMAIL PROTECTED]> [2002-11-20 23:10]:
> Solution: It doesn't.

I am duly admonished. You're right.

-- 
Regards,
Aristotle



Re: limit the list

2002-11-20 Thread A. Pagaltzis
* Dave Arnold <[EMAIL PROTECTED]> [2002-11-21 02:45]:
> $str = join ', ', @names;
> if ( length($str) > 90 )
> {
>   substr($str,rindex($str,",",90)) = ", etc.";
> }

Nice, and so obvious too - in retrospect, of course.

The only thing I don't like about it is it still joins
everything, even if it only needs 3 out of 10,000 elements.

-- 
Regards,
Aristotle



Re: limit the list

2002-11-20 Thread Dave Arnold
In message <[EMAIL PROTECTED]>
  "Selector, Lev Y" <[EMAIL PROTECTED]> wrote:

> Folks,
> 
> Simple question:
> Is there a more elegant way to express this:
>   an array of names is converted into a comma-separated list.
>   if the list gets to long - limit it and add ", etc." at the end.
> 
>   $str = join ', ', @names;
>   if (length($str)>90) {
> ($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;
>   }
> 
How about:

$str = join ', ', @names;
if ( length($str) > 90 )
{
  substr($str,rindex($str,",",90)) = ", etc.";
}

Dave.
-- 
No, the fact that it's an infinite loop doesn't mean the program doesn't
work; it just entered a state with which I was previously unfamiliar.
 Calum - Acorna, A.McCaffrey & M.Ball



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Paul Johnson
On Wed, Nov 20, 2002 at 08:46:25PM -, Peter Scott wrote:
>  [EMAIL PROTECTED] (Steven Lembark) writes:
> >-- Peter Scott <[EMAIL PROTECTED]>
> >> In article <[EMAIL PROTECTED]>,
> >>  [EMAIL PROTECTED] (Abigail) writes:
> >>> On Wed, Nov 20, 2002 at 11:42:43AM +0100, Bart Lateur wrote:
>  On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
> 
>  > sub commify
>  > {
>  >my ( $max, $sep, $end ) = ( shift, shift, shift );
>   ...
>  > }
> 
>  Wow! Hold it! Am I the only one who finds this absurd? More than one
>  shift on the same array in one single expressing, sounds like bad style
>  to me. Comments?
> >>>
> >>> Why is that bad style? Many times when people say it's bad style,
> >>> it's just a case of "beauty is in the eye of the beholder".
> >>
> >> It forces the reader to think about associativity and order of evaluation.
> >> If you've been bitten by unexpected outcomes before you might have to
> >> try it to make sure it does what you think.
> >>
> >> I've used shift, shift before, so I already know.  But it would be unfair
> >> to foist on a junior maintenance programmer, IMHO.
> >
> >Associativity? 
> 
> Of the commas.  A suitably paranoid programmer is going to wonder whether
> the results will occur in the expected order if they've not tried before.
> It won't occur to a novice.  And an expert will already know.  But someone
> in between may wonder.

How does an expert know?  You say you know because you have tried it.  I
think I know for the same reason, and because I have read the perly.y
and op.c, but I wouldn't want to stake anything particularly important
on it.

Can anyone point to any documentation which describes the order of
evaluation within a list?  The order of evaluation for the comma
operator in a scalar context is explicitly defined (which is just as
well), but I can't find such a guarantee in list context, which seems
strange if there were such a guarantee, since it would likely be found
immediately after the guarantee for scalar context in perlop.  Neither
can I find any tests for this, but I can find one core module which
depends on this left to right order of evaluation.

I suspect this order is unlikely to change because

  a. Perl tries hard to DWIM, and left to right is probably what most
 people mean.

  b. Perl tries hard to be backwards compatible, even when people have
 done things that they probably shouldn't have.

There are no guarantees on the order of evaluation within an expression.
Are there within a list?

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Peter Scott
[Posted and mailed]

In article <23862.1037824657@[192.168.200.4]>,
 [EMAIL PROTECTED] (Steven Lembark) writes:
>
>
>-- Peter Scott <[EMAIL PROTECTED]>
>
>> In article <[EMAIL PROTECTED]>,
>>  [EMAIL PROTECTED] (Abigail) writes:
>>> On Wed, Nov 20, 2002 at 11:42:43AM +0100, Bart Lateur wrote:
 On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:

 > sub commify
 > {
 >  my ( $max, $sep, $end ) = ( shift, shift, shift );
...
 > }

 Wow! Hold it! Am I the only one who finds this absurd? More than one
 shift on the same array in one single expressing, sounds like bad style
 to me. Comments?
>>>
>>> Why is that bad style? Many times when people say it's bad style,
>>> it's just a case of "beauty is in the eye of the beholder".
>>
>> It forces the reader to think about associativity and order of evaluation.
>> If you've been bitten by unexpected outcomes before you might have to
>> try it to make sure it does what you think.
>>
>> I've used shift, shift before, so I already know.  But it would be unfair
>> to foist on a junior maintenance programmer, IMHO.
>
>Associativity? 

Of the commas.  A suitably paranoid programmer is going to wonder whether
the results will occur in the expected order if they've not tried before.
It won't occur to a novice.  And an expert will already know.  But someone
in between may wonder.

>This just takes the first three items
>off the arguments (leaving the rest of it on @_),
>puts them on a list, and assigns it. I've had more
>problems with junior programmers botching the order
>of separate assignments (or more often deleting one
>out of the middle) than mis-understanding how shift
>works. Even fewer of the people walking around
>understand splice (which is where I came up with the
>list-of-shifts).

I don't think splice is that bad; it's a straightforward function.
You have to make sure the count matches the number of elements on the
left, just as you have to match the number of `shift's.

An argument this subjective is unlikely to reach a definitive resolution.

-- 
Peter Scott




Re: limit the list

2002-11-20 Thread Jonathan E. Paton
 --- "A. Pagaltzis" <[EMAIL PROTECTED]> wrote:
> * Steven Lembark <[EMAIL PROTECTED]> [2002-11-20 21:45]:
> > > but, the code said:
> > >
> > > $str = join ', ', @names;
> > > if (length($str)>90) {
> > > ($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;
> > > }
> > 
> > Original doesn't mention if the "etc." should fall w/in
> > the 90 char's or not. In that case you'd have to use a
> > length for the substr that leaves room for the trailer.
> 
> Noone paid attention, but it does. Look harder, it's there.
> (Solution: it does count toward the 90 char limit.)

I'm struggling to see why, can you enlighten me?  I understand
this:

($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;

Is a short hand for:

 $temp = substr($str, 0, 90);
 $temp =~ s/,[^,]*$/, etc./;
 $str = $temp;

or, in words... the substitution occurs en passant -
meaning that if you take 90 chars, and a comma is
the last character then the substitution ADDS
characters onto the string past char 90.

Thus, I stand to learn something if I'm wrong.  My
test code of:

my $str = ",";
($str = substr($str,0,1)) =~ s/,[^,]*$/, etc./;
print $str;

prints:

, etc.

which matches my understanding, and thus I'm extremely
interested to see your response.  Solution: It doesn't.

Jonathan Paton

=
s''! v+v+v+v+  J r e Ph+h+h+h+ !s`\x21`~`g,s`^ . | ~.*``mg,$v=q.
 P ! v-v-v-v-  u l r e r  h-h-h-   !12.,@.=m`.`g;do{$.=$2.$1,$.=~s`h
 E !   v+v+v+  s k e  h+h+ !`2`x,$.=~s`v`31`,print$.[$v+=$.]
 R ! v-v-  t H a c h  h-   !}while/([hv])([+-])/g;print"\xA"
 L ! A n o t   !';$..=$1while/([^!]*)$/mg;eval$.

__
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com



Re: limit the list

2002-11-20 Thread A. Pagaltzis
* Jonathan E. Paton <[EMAIL PROTECTED]> [2002-11-20 21:40]:
> You mean like the original poster wanted 90 chars, not 90
> elements?

Yes, and the first (working) snippet I posted does that,
with the least amount of tokens and highest efficiency and I
believe also greatest clarity of all solutions posted so far
(most of the initial of which honoured the 90 char intent) -
I'm open to suggestions if anyone disagrees.

TMTOWTDI, but not all ways are created equal and I believe
this is a case where few can sensibly be considered.

Looking back it isn't absolutely correct as it didn't take
the ", etc" into account as it should have, but that's
easy enough to fix.

-- 
Regards,
Aristotle



Re: limit the list

2002-11-20 Thread A. Pagaltzis
* Steven Lembark <[EMAIL PROTECTED]> [2002-11-20 21:45]:
> > but, the code said:
> >
> > $str = join ', ', @names;
> > if (length($str)>90) {
> > ($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;
> > }
> 
> Original doesn't mention if the "etc." should fall w/in
> the 90 char's or not. In that case you'd have to use a
> length for the substr that leaves room for the trailer.

Noone paid attention, but it does. Look harder, it's there.
(Solution: it does count toward the 90 char limit.)

-- 
Regards,
Aristotle



Re: AW: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Steven Lembark


-- Vladi Belperchinov-Shabanski <[EMAIL PROTECTED]>


On Wed, 20 Nov 2002 13:34:40 -
"Pense, Joachim" <[EMAIL PROTECTED]> wrote:


Bart Lateur [mailto:[EMAIL PROTECTED]] wrote:
(Mittwoch, 20. November 2002 11:43)

> On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
>
>> sub commify
>> {
>>	my ( $max, $sep, $end ) = ( shift, shift, shift );
>	...
>> }
>
> Wow! Hold it! Am I the only one who finds this absurd? More than one
> shift on the same array in one single expressing, sounds like bad style
> to me. Comments?

In one of my programs, this would be

sub commify {
my $max = shift;
my $sep = shift;
my $end = shift;

...
}


I use this form too. it is more explicit and gives nice way to comment:

sub commify {
 my $max = shift; # this is arg 1 blah
 my $sep = shift; # arg two blah
 my $end = shift; # arg III, actually takes hash reference to useless
data :)
 ...
}

which is better than

  my ( $max,  # ala
   $sep,  # bala
   $end ) # nica
  = @_;

imo.

it is matter of taste of cource...

my ( ... ) = @_;

has the only advantage to be ~20% faster for large number of function
call iterations.

finally:

sub nonsensessez
{
  my $s = $_[0];
  my $a = $_[1];
  my $k = $_[2];
  my $j = $_[3];
  my $l = $_[4];

  1;
}

combines the best from both forms above ( i.e. cna be commented, clean and
approx. as fast as `my ( ... ) = @_' thing.


Only to the extent that you are not processing the remainder
of @_ after taking the fixed parameters -- that or you have
to remember to use the proper offset in a foreach or array
slice to access the argument array in for/map/grep.


--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: limit the list

2002-11-20 Thread Steven Lembark


The text said:

"Is there a more elegant way to express this:
   an array of names is converted into a comma-separated list.
   if the list gets to long - limit it and add ", etc." at the end."

but, the code said:

$str = join ', ', @names;
if (length($str)>90) {
($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;
}

So, when in doubt... which do you believe?  "Use the source Luke"
springs to mind.  My fix is to repair the text with:

s/list g/line length g/;

Now, looking at the original code it looks quite sensible for
the task in hand.  Wonder if anyone else in the string of posters
noticed that... I'm too lazy to look back over the growing list of
mail on this topic though.


Extreme nit:

Original doesn't mention if the "etc." should fall w/in the
90 char's or not. In that case you'd have to use a length
for the substr that leaves room for the trailer.


--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Steven Lembark


-- "A. Pagaltzis" <[EMAIL PROTECTED]>


* Andrew Molyneux <[EMAIL PROTECTED]> [2002-11-20 18:25]:

A. Pagaltzis wrote:
> Enter splice.
>
> my ($max, $sep, $end) = splice @_, 0, 3;

That has brevity, certainly, but for legibility, I think I
prefer Steven's original (shift,shift,shift)


Really? I find the splice version a lot easier on the
easier on the eyes. But that's a matter of taste. How many
parameters do I have here?

shift, shift, shift, shift, shift, shift, shift, shift, shift, shift;
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
And this time?
splice @_, 0, 10;

Point in case, scalability.


And it obviously makes sense to use the splice at some point.
I am unlikely to use

	my ( $a ) = splice @_, 0, 1;

for example. Probably after 3-4 arguments I'd give up and
use splice or at some point pass the thing as a hash[ref]
and use named parameters.


--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: limit the list

2002-11-20 Thread Jonathan E. Paton
> > > join ', ', @array[0 .. $bound], @array > $bound ? 'etc' : ();
> > 
> > No, it's not.  In my way the effects on the array are
> > permanent, although that's probably not useful in this
> > instance
> 
> Point taken.
> 
> @array = @array[0 .. $bound], @array > $bound ? 'etc' : ();
> join ', ', @array;
> 
> :-)
> 
> > $#array# Gives last index of array, can be assigned to
> ># which increases or decreases the number of
> ># elements.
> > 
> > They didn't learn anything like that from yours... did they?  ;-)
> 
> @array[$min .. $max] # returns a list with all the elements
>  # between and including $array[$min]
>  # and $arrax[max]
> 
> You don't want to challenge me to nitpicking ;-P

You mean like the original poster wanted 90 chars, not 90 elements?

The text said:

"Is there a more elegant way to express this:
   an array of names is converted into a comma-separated list.
   if the list gets to long - limit it and add ", etc." at the end."

but, the code said:

$str = join ', ', @names;
if (length($str)>90) {
($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;
}

So, when in doubt... which do you believe?  "Use the source Luke"
springs to mind.  My fix is to repair the text with:

s/list g/line length g/;

Now, looking at the original code it looks quite sensible for
the task in hand.  Wonder if anyone else in the string of posters
noticed that... I'm too lazy to look back over the growing list of
mail on this topic though.

Jonathan Paton

=
s''! v+v+v+v+  J r e Ph+h+h+h+ !s`\x21`~`g,s`^ . | ~.*``mg,$v=q.
 P ! v-v-v-v-  u l r e r  h-h-h-   !12.,@.=m`.`g;do{$.=$2.$1,$.=~s`h
 E !   v+v+v+  s k e  h+h+ !`2`x,$.=~s`v`31`,print$.[$v+=$.]
 R ! v-v-  t H a c h  h-   !}while/([hv])([+-])/g;print"\xA"
 L ! A n o t   !';$..=$1while/([^!]*)$/mg;eval$.

__
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Steven Lembark


-- Philip Newton <[EMAIL PROTECTED]>


hat happens when you do

@a = qw( foo bar bletch blort bim bam blort );
my ( $a, $b, $c ) = @a;

?


Obviously a better example. Point is that $c is one
item on the list, but $a, $b, and $c are still on the
list. Given that the original code used the conteints
of @_ as fodder for the map it seemed more effective
to grab the values off the front rather than assign
them. So that using:

	my( $a, $b, $c ) = @_

	...

	map
	{
		...
	}
	@_

would not provide the same result as shifting the first
three items off of @_. You could obviously splice the
three items off in a void context after the assignment,
but at that point it seems easier to just assign the
shifts and be done with it in one place.



--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Steven Lembark


-- Peter Scott <[EMAIL PROTECTED]>


In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Abigail) writes:

On Wed, Nov 20, 2002 at 11:42:43AM +0100, Bart Lateur wrote:

On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:

> sub commify
> {
>	my ( $max, $sep, $end ) = ( shift, shift, shift );
	...
> }

Wow! Hold it! Am I the only one who finds this absurd? More than one
shift on the same array in one single expressing, sounds like bad style
to me. Comments?


Why is that bad style? Many times when people say it's bad style,
it's just a case of "beauty is in the eye of the beholder".


It forces the reader to think about associativity and order of evaluation.
If you've been bitten by unexpected outcomes before you might have to
try it to make sure it does what you think.

I've used shift, shift before, so I already know.  But it would be unfair
to foist on a junior maintenance programmer, IMHO.


Associativity? This just takes the first three items
off the arguments (leaving the rest of it on @_),
puts them on a list, and assigns it. I've had more
problems with junior programmers botching the order
of separate assignments (or more often deleting one
out of the middle) than mis-understanding how shift
works. Even fewer of the people walking around
understand splice (which is where I came up with the
list-of-shifts).



--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: limit the list

2002-11-20 Thread A. Pagaltzis
* Jonathan E. Paton <[EMAIL PROTECTED]> [2002-11-20 21:15]:
> > join ', ', @array[0 .. $bound], @array > $bound ? 'etc' : ();
> 
> No, it's not.  In my way the effects on the array are
> permanent, although that's probably not useful in this
> instance

Point taken.

@array = @array[0 .. $bound], @array > $bound ? 'etc' : ();
join ', ', @array;

:-)

> $#array# Gives last index of array, can be assigned to
># which increases or decreases the number of
># elements.
> 
> They didn't learn anything like that from yours... did they?  ;-)

@array[$min .. $max] # returns a list with all the elements
 # between and including $array[$min]
 # and $arrax[max]

You don't want to challenge me to nitpicking ;-P

-- 
Regards,
Aristotle



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread A. Pagaltzis
* Andrew Molyneux <[EMAIL PROTECTED]> [2002-11-20 18:25]:
> A. Pagaltzis wrote:
> > Enter splice.
> >
> > my ($max, $sep, $end) = splice @_, 0, 3;
> 
> That has brevity, certainly, but for legibility, I think I
> prefer Steven's original (shift,shift,shift)

Really? I find the splice version a lot easier on the
easier on the eyes. But that's a matter of taste. How many
parameters do I have here?

shift, shift, shift, shift, shift, shift, shift, shift, shift, shift;
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
And this time?
splice @_, 0, 10;

Point in case, scalability.

-- 
Regards,
Aristotle



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Philip Newton
On Wed, 20 Nov 2002 13:33:27 -0600, [EMAIL PROTECTED] (Steven Lembark)
wrote:

> -- Philip Newton <[EMAIL PROTECTED]>
> 
> > On Wed, 20 Nov 2002 10:35:11 -0600, [EMAIL PROTECTED] (Steven Lembark)
> > wrote:
> >
> >> -- Andrew Molyneux <[EMAIL PROTECTED]>
> >>
> >> > I'd probably do:
> >> > my ($max, $sep, $end) = @_;
> >>
> >> Yes, becuase if you did it this way you'd get $end equal
> >> to the integer coult of the number of list arguments passed
> >> plus one for the end value.
> >
> > Huh? $end gets assigned $_[2]. I'm not sure where you get an "integer
> > coult" from.
> 
> Look up what happens to arrays in a scalar context.

There's no scalar context involved as I understand it. Since you have
parentheses, it's a list context assignment.

> Or try in the debugger:
> 
> my ( $a, $b, $c ) = qw( foo bar bletch blort bim bam blort );
> 
> what do yo get for $c?

"bletch". Well, at least if I omit the "my" and do only ($a, $b, $c) =
qw( ... ). Possibly a scoping problem with my debugger: it works when I
type it straight into Perl, without the debugger.

Which version of Perl are you using? qw() used to be implemented using
split(), which pays attention to the number of arguments available for
assignment on the left-hand side (unlike other list assignment). I
believe this was changed round about 5.6.0 to expand qw(a b c) to the
list ('a', 'b', 'c') at compile-time rather than at run-time with a
hidden split. Maybe that's what caused you problems?

And as Aristotle said - that's not an array, so it's not the same thing.

What happens when you do

@a = qw( foo bar bletch blort bim bam blort );
my ( $a, $b, $c ) = @a;

?

Cheers,
Philip



Re: limit the list

2002-11-20 Thread Jonathan E. Paton
 --- "A. Pagaltzis" <[EMAIL PROTECTED]> wrote:
> * Jonathan E. Paton <[EMAIL PROTECTED]> [2002-11-20 20:51]:
> > if (@array > $bound) {
> > $array[$bound-1] = ", etc"; # Set element at boundary 
> > $#array = $bound-1; # Shorten array to boundary
> > }
> > 
> > print join ", ", @array;
> 
> Again, that's the same as:
> 
> join ', ', @array[0 .. $bound], @array > $bound ? 'etc' : ();

No, it's not.  In my way the effects on the array are permanent,
although that's probably not useful in this instance - however
there is educational value in my solution:

$#array# Gives last index of array, can be assigned to
   # which increases or decreases the number of
   # elements.

They didn't learn anything like that from yours... did they?  ;-)

Jonathan Paton

=
s''! v+v+v+v+  J r e Ph+h+h+h+ !s`\x21`~`g,s`^ . | ~.*``mg,$v=q.
 P ! v-v-v-v-  u l r e r  h-h-h-   !12.,@.=m`.`g;do{$.=$2.$1,$.=~s`h
 E !   v+v+v+  s k e  h+h+ !`2`x,$.=~s`v`31`,print$.[$v+=$.]
 R ! v-v-  t H a c h  h-   !}while/([hv])([+-])/g;print"\xA"
 L ! A n o t   !';$..=$1while/([^!]*)$/mg;eval$.

__
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Peter Scott
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] (Abigail) writes:
>On Wed, Nov 20, 2002 at 11:42:43AM +0100, Bart Lateur wrote:
>> On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
>> 
>> >sub commify
>> >{
>> >my ( $max, $sep, $end ) = ( shift, shift, shift );
>>  ...
>> >}
>> 
>> Wow! Hold it! Am I the only one who finds this absurd? More than one
>> shift on the same array in one single expressing, sounds like bad style
>> to me. Comments?
>
>Why is that bad style? Many times when people say it's bad style,
>it's just a case of "beauty is in the eye of the beholder". 

It forces the reader to think about associativity and order of evaluation.
If you've been bitten by unexpected outcomes before you might have to
try it to make sure it does what you think.

I've used shift, shift before, so I already know.  But it would be unfair
to foist on a junior maintenance programmer, IMHO.

-- 
Peter Scott




Re: AW: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Vladi Belperchinov-Shabanski
On Wed, 20 Nov 2002 13:34:40 -
"Pense, Joachim" <[EMAIL PROTECTED]> wrote:

> Bart Lateur [mailto:[EMAIL PROTECTED]] wrote:
> (Mittwoch, 20. November 2002 11:43)
> 
> >On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
> >
> >>sub commify
> >>{
> >>my ( $max, $sep, $end ) = ( shift, shift, shift );
> > ...
> >>}
> >
> >Wow! Hold it! Am I the only one who finds this absurd? More than one
> >shift on the same array in one single expressing, sounds like bad style
> >to me. Comments?
> 
> In one of my programs, this would be
> 
> sub commify {
> my $max = shift;
> my $sep = shift;
> my $end = shift;
> 
> ...
> }

I use this form too. it is more explicit and gives nice way to comment:

sub commify {
 my $max = shift; # this is arg 1 blah
 my $sep = shift; # arg two blah
 my $end = shift; # arg III, actually takes hash reference to useless data :)
 
 ...
}

which is better than

  my ( $max,  # ala
   $sep,  # bala
   $end ) # nica
  = @_;

imo.

it is matter of taste of cource...

my ( ... ) = @_;

has the only advantage to be ~20% faster for large number of function call iterations.

finally:

sub nonsensessez
{
  my $s = $_[0];
  my $a = $_[1];
  my $k = $_[2];
  my $j = $_[3];
  my $l = $_[4];

  1;
}

combines the best from both forms above ( i.e. cna be commented, clean and
approx. as fast as `my ( ... ) = @_' thing.

P! Vladi.

> 
> better or even worse in your view?
> 
> Joachim
> 


-- 
Vladi Belperchinov-Shabanski <[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
Personal home page at http://www.biscom.net/~cade
DataMax Ltd. http://www.datamax.bg
Too many hopes and dreams won't see the light...



msg02736/pgp0.pgp
Description: PGP signature


Re: AW: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Paul Johnson

Pense, Joachim said:
> Bart Lateur [mailto:[EMAIL PROTECTED]] wrote:
> (Mittwoch, 20. November 2002 11:43)
>
>>On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
>>
>>>sub commify
>>>{
>>> my ( $max, $sep, $end ) = ( shift, shift, shift );
>>  ...
>>>}
>>
>>Wow! Hold it! Am I the only one who finds this absurd? More than one
>> shift on the same array in one single expressing, sounds like bad style
>> to me. Comments?
>
> In one of my programs, this would be
>
> sub commify {
> my $max = shift;
> my $sep = shift;
> my $end = shift;
>
> ...
> }
>
> better or even worse in your view?

Better.  As far as I am aware, Perl doesn't provide any guarantees on the
order of evaluation of expressions between what C calls sequence points.

Of course, Perl doesn't have sequence points, and has only one interpreter
which generally works from left to right, but it's probably not good to
rely on that.  If nothing else it will screw you up when you write XS.  Or
maybe Perl 6 ;-)

What does this print?

  perl -le 'print $i += $i++ + ++$i'

Might is reasonably print something else?

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net






Re: limit the list

2002-11-20 Thread Vladi Belperchinov-Shabanski

hi,

my no golf, join, map, RE solution is: :)

@arr = qw( this is just a test );
$res;
for( @arr )
  {
  $res .= "$_,";
  $res .= "etc...,", last if length $res > 10;
  }
chop( $res );
print "$res\n";

the number 10 is matter of taste, 90 could be nice figure also...

P! Vladi.

On Tue, 19 Nov 2002 18:50:36 -0500
"Selector, Lev Y" <[EMAIL PROTECTED]> wrote:

> Folks,
> 
> Simple question:
> Is there a more elegant way to express this:
>   an array of names is converted into a comma-separated list.
>   if the list gets to long - limit it and add ", etc." at the end.
> 
>   $str = join ', ', @names;
>   if (length($str)>90) {
> ($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;
>   }
> 
> Warmest Regards, 
> Lev Selector, New York
> 


-- 
Vladi Belperchinov-Shabanski <[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
Personal home page at http://www.biscom.net/~cade
DataMax Ltd. http://www.datamax.bg
Too many hopes and dreams won't see the light...



msg02734/pgp0.pgp
Description: PGP signature


Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Jonathan E. Paton
> >> > I'd probably do:
> >> > my ($max, $sep, $end) = @_;
> >>
> >> Yes, becuase if you did it this way you'd get $end equal
> >> to the integer coult of the number of list arguments passed
> >> plus one for the end value.
> >
> > Huh? $end gets assigned $_[2]. I'm not sure where you get an "integer
> > coult" from.
> 
> Look up what happens to arrays in a scalar context.
> 
> Or try in the debugger:
> 
> my ( $a, $b, $c ) = qw( foo bar bletch blort bim bam blort );
> 
> what do yo get for $c?

Not what you expected... a slight change avoids this:

my ( $a, $b, $c, undef) = qw( foo bar bletch blort bim bam blort );

Jonathan Paton

=
s''! v+v+v+v+  J r e Ph+h+h+h+ !s`\x21`~`g,s`^ . | ~.*``mg,$v=q.
 P ! v-v-v-v-  u l r e r  h-h-h-   !12.,@.=m`.`g;do{$.=$2.$1,$.=~s`h
 E !   v+v+v+  s k e  h+h+ !`2`x,$.=~s`v`31`,print$.[$v+=$.]
 R ! v-v-  t H a c h  h-   !}while/([hv])([+-])/g;print"\xA"
 L ! A n o t   !';$..=$1while/([^!]*)$/mg;eval$.

__
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com



Re: limit the list

2002-11-20 Thread A. Pagaltzis
* Jonathan E. Paton <[EMAIL PROTECTED]> [2002-11-20 20:51]:
> if (@array > $bound) {
> $array[$bound-1] = ", etc"; # Set element at boundary 
> $#array = $bound-1; # Shorten array to boundary
> }
> 
> print join ", ", @array;

Again, that's the same as:

join ', ', @array[0 .. $bound], @array > $bound ? 'etc' : ();

> Now, this I *might* use in production code.  Of course, the
> obvious mistake is that space between the last element and
> the ", etc" part - but easily solved by using .= to append
> to the appropriate element.

No, it will look like "..blah, , etc" and that's easily
remedied by using just "etc" since the join adds its own
", " anyway.

-- 
Regards,
Aristotle



Re: limit the list

2002-11-20 Thread Michael G Schwern
On Wed, Nov 20, 2002 at 02:07:18AM -0600, Steven Lembark wrote:
> >my ($i, $total);
> >($total += length) < 90 ? $i++ : last for @ARGV;
> >$str = join ', ', @ARGV[0 .. $i];
> >$str .= ', etc' if $i < $#ARGV;
> 
> my $a = substr join( ',', @namz ), 0, 89;
> $a .= ',etc...' if length $a == 90;

That was my first try, but it doesn't work since the substr will often
cut off in the middle of a word and you wind up with things like 
"foo,foo,f,etc..."

Also, substr()'s LENGTH argument is just that.  Its not one-off like an
array index.  If you want 90 characters you say 90 not 89.


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>http://www.pobox.com/~schwern/
Perl Quality Assurance  <[EMAIL PROTECTED]> Kwalitee Is Job One
Good tidings, my native American Indian friend!  America will soon again
be yours!  Please accept 5th Avenue as an initial return!



RE: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Alistair . McGlinchy
> -Original Message-
> From: Steven Lembark [mailto:[EMAIL PROTECTED]] 
> -- Philip Newton <[EMAIL PROTECTED]>
> > On Wed, 20 Nov 2002 10:35:11 -0600, [EMAIL PROTECTED] (Steven 
> > Lembark) wrote:
> >> -- Andrew Molyneux <[EMAIL PROTECTED]>
> >>
> >> > I'd probably do:
> >> > my ($max, $sep, $end) = @_;
> >>
> >> Yes, becuase if you did it this way you'd get $end equal
> >> to the integer coult of the number of list arguments passed plus one 
> >> for the end value.
> >
> > Huh? $end gets assigned $_[2]. I'm not sure where you get an "integer 
> > coult" from.
> 
> Look up what happens to arrays in a scalar context.
> 
> Or try in the debugger:
> 
> my ( $a, $b, $c ) = qw( foo bar bletch blort bim bam blort );
> 
> what do yo get for $c?

What crack pipe yo' smokin' home's? $c ain't no integer coult, 'at $c be a
nasty "bletch".

d:\>perl -e "my ( $a, $b, $c ) = qw( foo bar bletch blort bim bam blort
);print$c"
bletch
d:\>perl -v

This is perl, v5.6.1 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)







---


Registered Office:
Marks & Spencer p.l.c
Michael House, Baker Street,
London, W1U 8EP
Registered No. 214436 in England and Wales.

Telephone (020) 7935 4422 
Facsimile (020) 7487 2670

www.marksandspencer.com

Please note that electronic mail may be monitored.

This e-mail is confidential. If you received it by mistake, please let us know and 
then delete it from your system; you should not copy, disclose, or distribute its 
contents to anyone nor act in reliance on this e-mail, as this is prohibited and may 
be unlawful.

The registered office of Marks and Spencer Financial Services PLC, Marks and Spencer 
Unit Trust Management Limited, Marks and Spencer Life Assurance Limited and Marks and 
Spencer Savings and Investments Limited is Kings Meadow, Chester, CH99 9FB.




Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread A. Pagaltzis
* Steven Lembark <[EMAIL PROTECTED]> [2002-11-20 20:51]:
> Look up what happens to arrays in a scalar context.
> 
> my ( $a, $b, $c ) = qw( foo bar bletch blort bim bam blort );
> 
> what do yo get for $c?

'bletch' - and that's not an array there.

-- 
Regards,
Aristotle



RE: limit the list

2002-11-20 Thread Alistair . McGlinchy
Not elegant but it is fun.

$str=   (grep {90 > length} (
(map  {join ", ", @names[0..$_], "etc."} 0..$#names),
join(", ",@names)   
))[-1]; 


---


Registered Office:
Marks & Spencer p.l.c
Michael House, Baker Street,
London, W1U 8EP
Registered No. 214436 in England and Wales.

Telephone (020) 7935 4422 
Facsimile (020) 7487 2670

www.marksandspencer.com

Please note that electronic mail may be monitored.

This e-mail is confidential. If you received it by mistake, please let us know and 
then delete it from your system; you should not copy, disclose, or distribute its 
contents to anyone nor act in reliance on this e-mail, as this is prohibited and may 
be unlawful.

The registered office of Marks and Spencer Financial Services PLC, Marks and Spencer 
Unit Trust Management Limited, Marks and Spencer Life Assurance Limited and Marks and 
Spencer Savings and Investments Limited is Kings Meadow, Chester, CH99 9FB.




Re: limit the list

2002-11-20 Thread Jonathan E. Paton
Hi all,

Lets get back to the original question:

 --- "Selector, Lev Y" <[EMAIL PROTECTED]> wrote:
> Folks,
> 
> Simple question:
> Is there a more elegant way to express this:
>   an array of names is converted into a comma-separated list.
>   if the list gets to long - limit it and add ", etc." at the end.
> 
>   $str = join ', ', @names;
>   if (length($str)>90) {
> ($str = substr($str,0,90)) =~ s/,[^,]*$/, etc./;
>   }

my answer:

if (@array > $bound) {
$array[$bound-1] = ", etc"; # Set element at boundary 
$#array = $bound-1; # Shorten array to boundary
}

print join ", ", @array;

Now, this I *might* use in production code.  Of course, the
obvious mistake is that space between the last element and
the ", etc" part - but easily solved by using .= to append
to the appropriate element.

E&OE

Jonathan Paton

=
s''! v+v+v+v+  J r e Ph+h+h+h+ !s`\x21`~`g,s`^ . | ~.*``mg,$v=q.
 P ! v-v-v-v-  u l r e r  h-h-h-   !12.,@.=m`.`g;do{$.=$2.$1,$.=~s`h
 E !   v+v+v+  s k e  h+h+ !`2`x,$.=~s`v`31`,print$.[$v+=$.]
 R ! v-v-  t H a c h  h-   !}while/([hv])([+-])/g;print"\xA"
 L ! A n o t   !';$..=$1while/([^!]*)$/mg;eval$.

__
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com



Re: limit the list

2002-11-20 Thread Steven Lembark


-- [EMAIL PROTECTED]


On Wed, 20 Nov 2002 12:30:46 -0600
Steven Lembark <[EMAIL PROTECTED]> wrote:




-- [EMAIL PROTECTED]

> join ', ', grep{ ($b.=$_) !~ /.{91}/ || ?.? && ($_ = 'etc.') } @names

Problem: the length does not include separators.

This will lead to over-length strings.


ack.


This is why my map used ($sum += $sln + length) < $cut to
get the string length.


--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: limit the list

2002-11-20 Thread badchoice
On Wed, 20 Nov 2002 12:30:46 -0600
Steven Lembark <[EMAIL PROTECTED]> wrote:

> 
> 
> -- [EMAIL PROTECTED]
> 
> > join ', ', grep{ ($b.=$_) !~ /.{91}/ || ?.? && ($_ = 'etc.') } @names
> 
> Problem: the length does not include separators.
> 
> This will lead to over-length strings.

ack.


assuming:
@names > 1; ($joined =~ /, /)
!/^.{90}(,|$)/

in other words completely useless:

$_ = join ', ', @a; s/[^,]*(?<=^.{90}).*/ etc./;


i just found a perl minigolf:
http://terje.dev.webon.net/wsp/pgas/score.pl?func=rules&hole=14




Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Steven Lembark


-- Philip Newton <[EMAIL PROTECTED]>


On Wed, 20 Nov 2002 10:35:11 -0600, [EMAIL PROTECTED] (Steven Lembark)
wrote:


-- Andrew Molyneux <[EMAIL PROTECTED]>

> I'd probably do:
> my ($max, $sep, $end) = @_;

Yes, becuase if you did it this way you'd get $end equal
to the integer coult of the number of list arguments passed
plus one for the end value.


Huh? $end gets assigned $_[2]. I'm not sure where you get an "integer
coult" from.


Look up what happens to arrays in a scalar context.

Or try in the debugger:

my ( $a, $b, $c ) = qw( foo bar bletch blort bim bam blort );

what do yo get for $c?



--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Philip Newton
On Wed, 20 Nov 2002 10:35:11 -0600, [EMAIL PROTECTED] (Steven Lembark)
wrote:

> -- Andrew Molyneux <[EMAIL PROTECTED]>
> 
> > I'd probably do:
> > my ($max, $sep, $end) = @_;
> 
> Yes, becuase if you did it this way you'd get $end equal
> to the integer coult of the number of list arguments passed
> plus one for the end value.

Huh? $end gets assigned $_[2]. I'm not sure where you get an "integer
coult" from.

> Notice the usage:
> 
>   my $string = commify 90, ', ', 'etc...', @names;

Those parameters get flattened into a list @_. @names doesn't know that
there's an assignment to a scalar in the subroutine; it gives up its
identity and becomes part of the list.

And anyway, it's a list context assignment.

The first three elements of @_ get assigned to $max, $sep, and $end
(respectively); all further elements get ignored.

Similar to

   my ($ss, $mm, $hh) = localtime;

or

   my ($foo, $bar, baz) = (1 .. 10);

> The other problem is that even if there were only three
> arguments being passed in you have to check the count
> before making the assignment and croak on @_ != 3 in
> order to avoid an extra parameter causing $end to
> become an integer count.

Why? IDGI.

Cheers,
Philip



Re: limit the list

2002-11-20 Thread Steven Lembark


-- Josh Goldberg <[EMAIL PROTECTED]>


On Wed, 20 Nov 2002 [EMAIL PROTECTED] wrote:


join ', ', grep{ ($b.=$_) !~ /.{91}/ || ?.? && ($_ = 'etc.') } @names



that didn't work in my test, but it gave me an idea with map.  I know, I
used goto, but when I try to break from the BLOCK it says i'm not really
in a block inside map, even when I set it up as a block like the POD
shows.


@names = (
'foo',
'bar',
'baz',
'boo',
'zor',
);

$lim = 2;
$i=0;

map{$i++<$lim?push @n,$_:$i!=$lim?{push @n,'etc.' and goto FOO}:''}@names;
FOO:
$names = join ', ',@n;
print "Names: $names\n";


Why waste a map if you aren't using the return? This would
probably be clearer as a for loop.

--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: limit the list

2002-11-20 Thread Steven Lembark


-- [EMAIL PROTECTED]


join ', ', grep{ ($b.=$_) !~ /.{91}/ || ?.? && ($_ = 'etc.') } @names


Problem: the length does not include separators.

This will lead to over-length strings.


--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: limit the list

2002-11-20 Thread A. Pagaltzis
* Josh Goldberg <[EMAIL PROTECTED]> [2002-11-20 19:20]:
> map{$i++<$lim?push @n,$_:$i!=$lim?{push @n,'etc.' and goto FOO}:''}@names;

Sigh. map in void context and goto where a for would have
done. And it doesn't even conform to the specs, you were
asked to provide a string with less than 90 characters that
should end with "etc" if there are more items than
displayed. As far as I can tell, all you're doing is

@n = @names[0..$lim-1];
push @n, "etc" if @names > $lim;

-- 
Regards,
Aristotle



Re: limit the list

2002-11-20 Thread Josh Goldberg
On Wed, 20 Nov 2002 [EMAIL PROTECTED] wrote:

> join ', ', grep{ ($b.=$_) !~ /.{91}/ || ?.? && ($_ = 'etc.') } @names
>
 
that didn't work in my test, but it gave me an idea with map.  I know, I
used goto, but when I try to break from the BLOCK it says i'm not really
in a block inside map, even when I set it up as a block like the POD
shows.


@names = (
'foo',
'bar',
'baz',
'boo',
'zor',
);

$lim = 2;
$i=0;

map{$i++<$lim?push @n,$_:$i!=$lim?{push @n,'etc.' and goto FOO}:''}@names;
FOO:
$names = join ', ',@n;
print "Names: $names\n";




Re: limit the list

2002-11-20 Thread badchoice
join ', ', grep{ ($b.=$_) !~ /.{91}/ || ?.? && ($_ = 'etc.') } @names



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Andrew Molyneux
Steven Lembark wrote:
> Yes, becuase if you did it this way you'd get $end equal
> to the integer coult of the number of list arguments passed
> plus one for the end value. Notice the usage:
>
> my $string = commify 90, ', ', 'etc...', @names;
>

D'oh! (slaps head).  Serves me right for not reading your code properly.  I
blame a lack of caffeine.

A. Pagaltzis wrote:
> Enter splice.
>
> my ($max, $sep, $end) = splice @_, 0, 3;

That has brevity, certainly, but for legibility, I think I prefer Steven's
original (shift,shift,shift)

Andrew




Re: limit the list

2002-11-20 Thread A. Pagaltzis
* Steven Lembark <[EMAIL PROTECTED]> [2002-11-20 17:35]:
> > Wasted work. Two comparisons per element and you don't bail
> > once you've filled your available space.
> 
> Two comparisons on a short list, at which point the remainder
> of them are a simple comparison to get a () out, which ends
> up as a null operation. And they are separate.

Still one needless comparison and null op where you could
have bailed and done nothing. May I also be so bold as to
suggest that I find this version pretty unclear - I'm quite
sure the for() approach I took breaks the task down in a way
it's instantly obvious what it's doing. And that's what I
always strive for - in this lucky case, it's more efficient
to boot.

-- 
Regards,
Aristotle



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread A. Pagaltzis
* Steven Lembark <[EMAIL PROTECTED]> [2002-11-20 17:35]:
> Yes, becuase if you did it this way you'd get $end equal
> to the integer coult of the number of list arguments passed
> plus one for the end value. Notice the usage:
> 
>   my $string = commify 90, ', ', 'etc...', @names;
> 
> The other problem is that even if there were only three
> arguments being passed in you have to check the count
> before making the assignment and croak on @_ != 3 in
> order to avoid an extra parameter causing $end to
> become an integer count.

Enter splice.

my ($max, $sep, $end) = splice @_, 0, 3;

-- 
Regards,
Aristotle



Re: AW: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Steven Lembark


-- "Pense, Joachim" <[EMAIL PROTECTED]>


Von: Moran, Matthew [mailto:[EMAIL PROTECTED]]
Gesendet am: Mittwoch, 20. November 2002 14:41
Joachim suggested:



sub commify {
my $max = shift;
my $sep = shift;
my $end = shift;

...
}

better or even worse in your view?


Clearer, but just as bad IMHO. I've always done it as



sub subroutine(
	my ($list, &of, $variables) = @_;
#	rest of code here
}



It's how it's always shown in the Cookbook & what have you.


Not *always*, I've seen my version explicitly suggested somewhere. The
advantage in my eyes is that you consume the input parameters so they are
gone when you read them. It is also easier to check in the end if there
are any left so too many were supplied.

My formatting makes the parameters easy to comment. The overhead of the
shift should not be relevant.

But anyway, the parameter mechanism I'd prefer before positional
parameters would be

sub mysub {
my %params = (
thiskey=> 'default1',
anotherkey => 'default2',
@_
);

# do something
}

without any shifting and consuming and whatever.

fwiw, Joachim



Did anyone bother to look at the original code. It takes
the remainder of @_ after the fixed parameters have been
shifted off and pushes them through a map. In that case
assigning @_ to anything will probably not be what you want.


--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: AW: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Steven Lembark


-- "Pense, Joachim" <[EMAIL PROTECTED]>


Bart Lateur [mailto:[EMAIL PROTECTED]] wrote:
(Mittwoch, 20. November 2002 11:43)


On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:


sub commify
{
	my ( $max, $sep, $end ) = ( shift, shift, shift );

	...

}


Wow! Hold it! Am I the only one who finds this absurd? More than one
shift on the same array in one single expressing, sounds like bad style
to me. Comments?


In one of my programs, this would be

sub commify {
my $max = shift;
my $sep = shift;
my $end = shift;


Until someone does

	 my $sep = shift;
	 my $max = shift;
	 my $end = shift;

or

	 my $sep = shift;
	 my $max = shift;


to it. Especially the former has caused people who work for
me pain, which is why I adopted the one-line method. That
or splice.



--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: limit the list

2002-11-20 Thread Steven Lembark


-- "A. Pagaltzis" <[EMAIL PROTECTED]>


* Steven Lembark <[EMAIL PROTECTED]> [2002-11-20 12:47]:

	map
	{
		$sum < $cut ?

			( ($sum += $sln + length) < $cut ? $_ : $end )
			:
			()
	}


Wasted work. Two comparisons per element and you don't bail
once you've filled your available space.


Two comparisons on a short list, at which point the remainder
of them are a simple comparison to get a () out, which ends
up as a null operation. And they are separate.





--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Steven Lembark


-- Andrew Molyneux <[EMAIL PROTECTED]>


Wow! Hold it! Am I the only one who finds this absurd? More than one
shift on the same array in one single expressing, sounds like bad style
to me. Comments?

You're not the only one.  I'd probably do:
my ($max, $sep, $end) = @_;

 but I'd love to know if Steven had a specific reason for doing it the
other way.


Yes, becuase if you did it this way you'd get $end equal
to the integer coult of the number of list arguments passed
plus one for the end value. Notice the usage:

	my $string = commify 90, ', ', 'etc...', @names;

The other problem is that even if there were only three
arguments being passed in you have to check the count
before making the assignment and croak on @_ != 3 in
order to avoid an extra parameter causing $end to
become an integer count.


--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Steven Lembark


-- Bart Lateur <[EMAIL PROTECTED]>


On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:


sub commify
{
	my ( $max, $sep, $end ) = ( shift, shift, shift );

	...

}


Wow! Hold it! Am I the only one who finds this absurd? More than one
shift on the same array in one single expressing, sounds like bad style
to me. Comments?


I've been using multiple shift's for years. Puts all of
the local var's in one place and makes it much harder for
edit errors to remove or re-order the parameters.


--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: AW: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Jeremy Impson
On Wed, 20 Nov 2002, Pense, Joachim wrote:

> Bart Lateur [mailto:[EMAIL PROTECTED]] wrote:
> (Mittwoch, 20. November 2002 11:43)
> 
> >On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
> >
> >>sub commify
> >>{
> >>my ( $max, $sep, $end ) = ( shift, shift, shift );
> > ...
> >>}
> >
> >Wow! Hold it! Am I the only one who finds this absurd? More than one
> >shift on the same array in one single expressing, sounds like bad style
> >to me. Comments?
> 
> In one of my programs, this would be
> 
> sub commify {
> my $max = shift;
> my $sep = shift;
> my $end = shift;
> 
> ...
> }
> 
> better or even worse in your view?

Better, but only if it makes sense to conditionally take values off the 
calling stack, like this contrived example:

sub foo {
my $type = shift;
my $arg1 = shift;

my $arg2 = 0;

if ($type == 'APIv2') {
$arg2 = shift;
}

return $arg1 + $arg2;
}

A variation of this would be if, depending on the type, you might want 
different names for a given element on the calling stack:

sub foo {
my $type = shift;
my $arg1 = shift;

if ($type == 'APIv2') {
my $adder = shift;
return $arg1 + $adder
} elsif ($type == 'APIv3') {
my $subtractor = shift;
return $arg1 - $subtractor;
}

return $arg1;
}


You might also do something like this to illustrate that depending on the
value of the first argument, we may not even care about any of the rest.

sub foo {
my $type = shift;

if ($type == 'APIv3') {
carp "APIv3 unsupported\n";
return;
}

# perhaps do computationally intensive or transactional stuff here
# ..

my $arg1 = shift;
my $arg2 = 0;

if ($type == 'APIv2') {
$arg2 = shift;
}

return $arg1 + $arg2;
}

Granted, there's no performance benefit, but since certain variables
aren't introduced until after the conditional return case, it definitively
shows that those values aren't needed unless we pass the condition.

I think that using 'shift' as a little faucet that gives you stuff as you
need it is rather elegant.

--Jeremy

-- 

Jeremy Impson
Sr. Associate Network Engineer
Investigative Research & Marketing
Lockheed Martin Systems Integration
email: [EMAIL PROTECTED]
phone: 607-751-5618
fax:   607-751-6025





Re: AW: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Jonathan E. Paton
 --- "Pense, Joachim" <[EMAIL PROTECTED]> wrote: > >Von: Moran, Matthew
[mailto:[EMAIL PROTECTED]]
> >Gesendet am: Mittwoch, 20. November 2002 14:41
> >Joachim suggested:
>  
> >> sub commify {
> >> my $max = shift;
> >> my $sep = shift;
> >> my $end = shift;
> >> 
> >> ...
> >> }
> >> 
> >> better or even worse in your view?
> >
> >Clearer, but just as bad IMHO. I've always done it as
> 
> >sub subroutine(
> > my ($list, &of, $variables) = @_;
> >#rest of code here
> >}
> 
> >It's how it's always shown in the Cookbook & what have you.
> 
> Not *always*, I've seen my version explicitly suggested somewhere. The
> advantage in my eyes is that you consume the input parameters so they are
> gone when you read them. It is also easier to check in the end if there are
> any left so too many were supplied.

You mean you saw it in the holy book itself: "Programming Perl"
(aka The Camel).  Looking at page 374 we find:

sub TIEARRAY {
my $class = shift;
my $bound = shift;
confess "usage: tie(\@array, 'BoundedArray', max_subscript)"
if @_ || $bound =~ /\D/;
return bless { BOUND => $bound, DATA => [] }, $class;
}

To bring this back inline with the original discussion, I was thinking the
problem could be solved with a tied array.  Starting at 372 it shouldn't
take long to implement the required code - left as an exercise for the
reader.

Jonathan Paton

=
s''! v+v+v+v+  J r e Ph+h+h+h+ !s`\x21`~`g,s`^ . | ~.*``mg,$v=q.
 P ! v-v-v-v-  u l r e r  h-h-h-   !12.,@.=m`.`g;do{$.=$2.$1,$.=~s`h
 E !   v+v+v+  s k e  h+h+ !`2`x,$.=~s`v`31`,print$.[$v+=$.]
 R ! v-v-  t H a c h  h-   !}while/([hv])([+-])/g;print"\xA"
 L ! A n o t   !';$..=$1while/([^!]*)$/mg;eval$.

__
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com



RE: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Bernie Cosell
On 20 Nov 2002 at 13:43, Moran, Matthew wrote:

> Abigail wondered:
> 
> > Why is that bad style? Many times when people say it's bad style,
> > it's just a case of "beauty is in the eye of the beholder". 
> > 
> 
> Strikes me that instead of using one move to assign the variables, it's
> using three. 

Just so.  If the *intent* of the code is to remove the first three elements 
from @_ and assign them to variables[*] isn't the clearest way to express the 
intent just to do:

   my ($a, $b, $c) = splice (@_,0,3)

It seems to express the *exact* semantics desired [remove the elements from @_ 
and assign those to the vbls), and to my eye does it more clearly than (shift, 
shift, shift) does.

[*]  NB: this has *different* semantics than doing my ($v1, $v2,
$v3) = @_ -- I'm assuming here that the modification to @_ is
actually necessary [if not, then it is not bad form on syntatic
grounds, but because it is doing something unnecessary and
potentially confusing]. 

/Bernie\

-- 
Bernie Cosell Fantasy Farm Fibers
mailto:[EMAIL PROTECTED] Pearisburg, VA
-->  Too many people, too few sheep  <--  




AW: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Pense, Joachim
>Von: Moran, Matthew [mailto:[EMAIL PROTECTED]]
>Gesendet am: Mittwoch, 20. November 2002 14:41
>Joachim suggested:
 
>> sub commify {
>> my $max = shift;
>> my $sep = shift;
>> my $end = shift;
>> 
>> ...
>> }
>> 
>> better or even worse in your view?
>
>Clearer, but just as bad IMHO. I've always done it as

>sub subroutine(
>   my ($list, &of, $variables) = @_;
>#  rest of code here
>}

>It's how it's always shown in the Cookbook & what have you.

Not *always*, I've seen my version explicitly suggested somewhere. The
advantage in my eyes is that you consume the input parameters so they are
gone when you read them. It is also easier to check in the end if there are
any left so too many were supplied.

My formatting makes the parameters easy to comment. The overhead of the
shift should not be relevant.

But anyway, the parameter mechanism I'd prefer before positional parameters
would be

sub mysub {
my %params = (
thiskey=> 'default1',
anotherkey => 'default2',
@_
);

# do something
}

without any shifting and consuming and whatever. 

fwiw, Joachim



RE: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Moran, Matthew
Abigail wondered:

> Why is that bad style? Many times when people say it's bad style,
> it's just a case of "beauty is in the eye of the beholder". 
> 

Strikes me that instead of using one move to assign the variables, it's
using three. 

Matt



" This message contains information that may be privileged or confidential and 
is the property of the Cap Gemini Ernst & Young Group. It is intended only for 
the person to whom it is addressed. If you are not the intended recipient, you 
are not authorized to read, print, retain, copy, disseminate, distribute, or use 
this message or any part thereof. If you receive this message in error, please 
notify the sender immediately and delete all copies of this message ".





RE: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Moran, Matthew
Joachim suggested:

> >Wow! Hold it! Am I the only one who finds this absurd? More than one
> >shift on the same array in one single expressing, sounds 
> like bad style
> >to me. Comments?
> 
> In one of my programs, this would be
> 
> sub commify {
> my $max = shift;
> my $sep = shift;
> my $end = shift;
> 
> ...
> }
> 
> better or even worse in your view?

Clearer, but just as bad IMHO. I've always done it as

sub subroutine(
my ($list, &of, $variables) = @_;
#   rest of code here
}

It's how it's always shown in the Cookbook & what have you.

Matt



" This message contains information that may be privileged or confidential and 
is the property of the Cap Gemini Ernst & Young Group. It is intended only for 
the person to whom it is addressed. If you are not the intended recipient, you 
are not authorized to read, print, retain, copy, disseminate, distribute, or use 
this message or any part thereof. If you receive this message in error, please 
notify the sender immediately and delete all copies of this message ".





Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Abigail
On Wed, Nov 20, 2002 at 11:42:43AM +0100, Bart Lateur wrote:
> On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
> 
> >sub commify
> >{
> > my ( $max, $sep, $end ) = ( shift, shift, shift );
>   ...
> >}
> 
> Wow! Hold it! Am I the only one who finds this absurd? More than one
> shift on the same array in one single expressing, sounds like bad style
> to me. Comments?


Why is that bad style? Many times when people say it's bad style,
it's just a case of "beauty is in the eye of the beholder". 

However, sometimes a style is bad because it's error-prone, 
confusing, similar to common idiom but doing something else,
or inefficient. But I don't think any of them applies to this
particular example.

Bart, can you explain why this is bad style? Or is it just your
personal preference?



Abigail



AW: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Pense, Joachim
Bart Lateur [mailto:[EMAIL PROTECTED]] wrote:
(Mittwoch, 20. November 2002 11:43)

>On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
>
>>sub commify
>>{
>>  my ( $max, $sep, $end ) = ( shift, shift, shift );
>   ...
>>}
>
>Wow! Hold it! Am I the only one who finds this absurd? More than one
>shift on the same array in one single expressing, sounds like bad style
>to me. Comments?

In one of my programs, this would be

sub commify {
my $max = shift;
my $sep = shift;
my $end = shift;

...
}

better or even worse in your view?

Joachim



Re: limit the list

2002-11-20 Thread A. Pagaltzis
* Steven Lembark <[EMAIL PROTECTED]> [2002-11-20 12:47]:
>   map
>   {
>   $sum < $cut ?
> 
>   ( ($sum += $sln + length) < $cut ? $_ : $end )
>   :
>   ()
>   }

Wasted work. Two comparisons per element and you don't bail
once you've filled your available space.

On that note, this is a deficiency of map() vs for() that
I've often mourned - it's possible but I don't want to get
used to things like eval { map { .. die() } @LIST }; (goto
should work too, but that's even more atrocious). Maybe all
flow control being "funny looking exceptions" in Perl 6 will
allow us to retrofit it without crutches if it's not in
there by default.

-- 
Regards,
Aristotle



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread A. Pagaltzis
* Andrew Molyneux <[EMAIL PROTECTED]> [2002-11-20 12:47]:
> You're not the only one.  I'd probably do:
> my ($max, $sep, $end) = @_;

me too

>  but I'd love to know if Steven had a specific reason
> for doing it the other way.

There doesn't seem to be any here.

-- 
Regards,
Aristotle



Re: Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Andrew Molyneux
> Wow! Hold it! Am I the only one who finds this absurd? More than one
> shift on the same array in one single expressing, sounds like bad style
> to me. Comments?
You're not the only one.  I'd probably do:
my ($max, $sep, $end) = @_;

 but I'd love to know if Steven had a specific reason for doing it the
other way.

Andrew

- Original Message -
From: "Bart Lateur" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, November 20, 2002 10:42 AM
Subject: Function parameter passing (was: Re: limit the list)


> On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
>
> >sub commify
> >{
> > my ( $max, $sep, $end ) = ( shift, shift, shift );
> ...
> >}
>
> Wow! Hold it! Am I the only one who finds this absurd? More than one
> shift on the same array in one single expressing, sounds like bad style
> to me. Comments?
>
> --
> Bart.
>




Function parameter passing (was: Re: limit the list)

2002-11-20 Thread Bart Lateur
On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:

>sub commify
>{
>   my ( $max, $sep, $end ) = ( shift, shift, shift );
...
>}

Wow! Hold it! Am I the only one who finds this absurd? More than one
shift on the same array in one single expressing, sounds like bad style
to me. Comments?

-- 
Bart.



Re: limit the list

2002-11-20 Thread Steven Lembark


-- Michael G Schwern <[EMAIL PROTECTED]>


On Wed, Nov 20, 2002 at 02:07:18AM -0600, Steven Lembark wrote:

> my ($i, $total);
> ($total += length) < 90 ? $i++ : last for @ARGV;
> $str = join ', ', @ARGV[0 .. $i];
> $str .= ', etc' if $i < $#ARGV;

my $a = substr join( ',', @namz ), 0, 89;
$a .= ',etc...' if length $a == 90;


That was my first try, but it doesn't work since the substr will often
cut off in the middle of a word and you wind up with things like
"foo,foo,f,etc..."

Also, substr()'s LENGTH argument is just that.  Its not one-off like an
array index.  If you want 90 characters you say 90 not 89.


With caffene...

e.g., my $string = commify 90, ', ', 'etc...', @namz;

sub commify
{
	my ( $max, $sep, $end ) = ( shift, shift, shift );

	my $sum = 0;

	# if $end can dangle use $cut = $max.

	my $cut = $max - length $sep;

	my $sln = length $sep;

	join $sep,
	map
	{
		$sum < $cut ?

			( ($sum += $sln + length) < $cut ? $_ : $end )
			:
			()
	}
	@_
}



--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582



Re: limit the list

2002-11-20 Thread Steven Lembark


my ($i, $total);
($total += length) < 90 ? $i++ : last for @ARGV;
$str = join ', ', @ARGV[0 .. $i];
$str .= ', etc' if $i < $#ARGV;


my $a = substr join( ',', @namz ), 0, 89;
$a .= ',etc...' if length $a == 90;


--
Steven Lembark   2930 W. Palmer
Workhorse Computing   Chicago, IL 60647
   +1 800 762 1582