[31415] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2667 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Nov 7 21:10:18 2009

Date: Sat, 7 Nov 2009 18:09:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 7 Nov 2009     Volume: 11 Number: 2667

Today's topics:
    Re: FAQ 4.50 How do I select a random element from an a <brian.d.foy@gmail.com>
    Re: FAQ 4.50 How do I select a random element from an a <m0t0rbr3th@gmail.com>
        remove elements from array ref <user@example.net>
    Re: remove elements from array ref <OJZGSRPBZVCX@spammotel.com>
    Re: remove elements from array ref sln@netherlands.com
    Re: remove elements from array ref <uri@StemSystems.com>
    Re: remove elements from array ref <martien.verbruggen@invalid.see.sig>
    Re: remove elements from array ref sln@netherlands.com
    Re: remove elements from array ref <nospam-abuse@ilyaz.org>
    Re: remove elements from array ref <someone@example.com>
    Re: remove elements from array ref sln@netherlands.com
    Re: remove elements from array ref sln@netherlands.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 06 Nov 2009 13:03:53 -0600
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 4.50 How do I select a random element from an array?
Message-Id: <061120091303535357%brian.d.foy@gmail.com>

In article
<7f36e87f-d06b-4479-afdb-62e6d92cd5b6@k4g2000yqb.googlegroups.com>,
m!thun <m0t0rbr3th@gmail.com> wrote:

> On Nov 4, 11:00 am, PerlFAQ Server <br...@theperlreview.com> wrote:

> >             my $element = $array[ rand @array ];

> Wouldn't this be safer?
> 
> $element = $array[ int( rand @array ) ];

Array and slice indices are already converted to integers for you. :)


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

Date: Fri, 6 Nov 2009 12:08:50 -0800 (PST)
From: "m!thun" <m0t0rbr3th@gmail.com>
Subject: Re: FAQ 4.50 How do I select a random element from an array?
Message-Id: <69fd9b4a-60d5-44fd-8511-348739d09d4c@f16g2000yqm.googlegroups.com>

On Nov 6, 2:03=A0pm, brian d foy <brian.d....@gmail.com> wrote:
> In article
> <7f36e87f-d06b-4479-afdb-62e6d92cd...@k4g2000yqb.googlegroups.com>,
>
> m!thun <m0t0rbr...@gmail.com> wrote:
> > On Nov 4, 11:00=A0am, PerlFAQ Server <br...@theperlreview.com> wrote:
> > > =A0 =A0 =A0 =A0 =A0 =A0 my $element =3D $array[ rand @array ];
> > Wouldn't this be safer?
>
> > $element =3D $array[ int( rand @array ) ];
>
> Array and slice indices are already converted to integers for you. :)

I had no idea that it did that until I tried the code in the FAQ. The
reason I asked this question is because perlfunc rand() says "Apply int
() to the value returned by rand() if you want random integers instead
of random fractional numbers". Always having used integers for an
array index/slice, I assumed I would want a random integer instead of
a fraction. Anyways, now I know that Perl handles that internally.


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

Date: Sat, 07 Nov 2009 14:47:41 -0500
From: monkeys paw <user@example.net>
Subject: remove elements from array ref
Message-Id: <_8GdnRRI7Y5YU2jXnZ2dnUVZ_tmdnZ2d@insightbb.com>

The folllowing code is supposed to remove entries from
@return_set that end in '000'. It leaves an undef in
the array instead of removing the whole element. What
fix would be best to achieve an array without 'undef'
elements? Thanks for any help!


### CODE TO REMOVE records  with id =~ /000$/
my @return_set = (
   {id => 'CA2345H000'},
   {id => 'CA2345H100'},
   {id => 'CA2345H030'},
   {id => 'CA235H000'},
   {id => 'CA234H001'},
);

_exclude_special_session(\@return_set);

use Data::Dumper;die 'DDDEBUG' .  Dumper(\@return_set);

sub _exclude_special_session {
     my $recordset = shift;
     my $i;
     for my $record (@{$recordset}) {
         if ($record->{id} =~ /000$/) {
             delete $recordset->[$i];
         }
         $i++;
     }
}


##### HERE IS THE OUTPUT
DDDEBUG$VAR1 = [
           undef,
           {
             'id' => 'CA2345H100'
           },
           {
             'id' => 'CA2345H030'
           },
           undef,
           {
             'id' => 'CA234H001'
           }
         ];


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

Date: Sat, 07 Nov 2009 21:02:13 +0100
From: "Jochen Lehmeier" <OJZGSRPBZVCX@spammotel.com>
Subject: Re: remove elements from array ref
Message-Id: <op.u21i9zhwmk9oye@frodo>

On Sat, 07 Nov 2009 20:47:41 +0100, monkeys paw <user@example.net> wrote:

> The folllowing code is supposed to remove entries from
> @return_set that end in '000'. It leaves an undef in
> the array instead of removing the whole element. What
> fix would be best to achieve an array without 'undef'
> elements? Thanks for any help!

@new_set = grep { $_->{id} !~ m/000$/ } @return_set;


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

Date: Sat, 07 Nov 2009 12:37:25 -0800
From: sln@netherlands.com
Subject: Re: remove elements from array ref
Message-Id: <gimbf51nljcje9hp3gfcfhn50pvtqntl0p@4ax.com>

On Sat, 07 Nov 2009 14:47:41 -0500, monkeys paw <user@example.net> wrote:

>The folllowing code is supposed to remove entries from
>@return_set that end in '000'. It leaves an undef in
>the array instead of removing the whole element. What
>fix would be best to achieve an array without 'undef'
>elements? Thanks for any help!
>
>
>### CODE TO REMOVE records  with id =~ /000$/
>my @return_set = (
>   {id => 'CA2345H000'},
>   {id => 'CA2345H100'},
>   {id => 'CA2345H030'},
>   {id => 'CA235H000'},
>   {id => 'CA234H001'},
>);
>
>_exclude_special_session(\@return_set);
>
>use Data::Dumper;die 'DDDEBUG' .  Dumper(\@return_set);
>
>sub _exclude_special_session {
>     my $recordset = shift;
>     my $i;
>     for my $record (@{$recordset}) {
>         if ($record->{id} =~ /000$/) {

              splice (@{$recordset},$i,1);

>         }
>         $i++;
>     }
>}
>
>

-sln



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

Date: Sat, 07 Nov 2009 15:41:32 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: remove elements from array ref
Message-Id: <878weicbk3.fsf@quad.sysarch.com>

>>>>> "mp" == monkeys paw <user@example.net> writes:

  mp> The folllowing code is supposed to remove entries from
  mp> @return_set that end in '000'. It leaves an undef in
  mp> the array instead of removing the whole element. What
  mp> fix would be best to achieve an array without 'undef'
  mp> elements? Thanks for any help!

  mp>             delete $recordset->[$i];

did you read the docs on delete? it is meant for use on hashes and the
docs say it leaves an undef in an array. to actually remove an element
or a set of elements from an array, splice is used. as someone else
posted, your problem is best solved by grep.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Sun, 8 Nov 2009 08:05:07 +1100
From: Martien Verbruggen <martien.verbruggen@invalid.see.sig>
Subject: Re: remove elements from array ref
Message-Id: <3en4dh.5e7.ln@news.heliotrope.home>

On Sat, 07 Nov 2009 12:37:25 -0800,
	sln@netherlands.com <sln@netherlands.com> wrote:
> On Sat, 07 Nov 2009 14:47:41 -0500, monkeys paw <user@example.net> wrote:
>
>>sub _exclude_special_session {
>>     my $recordset = shift;
>>     my $i;
>>     for my $record (@{$recordset}) {
>>         if ($record->{id} =~ /000$/) {
>
>               splice (@{$recordset},$i,1);
>
>>         }
>>         $i++;
>>     }

From the perlsyn documentation:

       If any part of LIST is an array, "foreach" will get very confused if
       you add or remove elements within the loop body, for example with
       "splice".   So don’t do that.

Martien
-- 
                             | 
Martien Verbruggen           | Since light travels faster than sound,
first.last@heliotrope.com.au | is that why some people appear bright
                             | until you hear them speak?


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

Date: Sat, 07 Nov 2009 15:41:19 -0800
From: sln@netherlands.com
Subject: Re: remove elements from array ref
Message-Id: <b81cf55dd66jnam8co0a8cn13l8amj8v1b@4ax.com>

On Sun, 8 Nov 2009 08:05:07 +1100, Martien Verbruggen <martien.verbruggen@invalid.see.sig> wrote:

>On Sat, 07 Nov 2009 12:37:25 -0800,
>	sln@netherlands.com <sln@netherlands.com> wrote:
>> On Sat, 07 Nov 2009 14:47:41 -0500, monkeys paw <user@example.net> wrote:
>>
>>>sub _exclude_special_session {
>>>     my $recordset = shift;
>>>     my $i;
>>>     for my $record (@{$recordset}) {
>>>         if ($record->{id} =~ /000$/) {
>>
>>               splice (@{$recordset},$i,1);
>>
>>>         }
>>>         $i++;
>>>     }
>
>From the perlsyn documentation:
>
>       If any part of LIST is an array, "foreach" will get very confused if
>       you add or remove elements within the loop body, for example with
>       "splice".   So don’t do that.
>
>Martien

Oh, I didn't see the LIST context.
Either one of these then.

for ( my $i=0; $i<@{$recordset}; $i++ ) {
  $recordset->[$i]{id} =~ /000$/ and splice (@{$recordset},$i--,1);
}

my $i = 0;
while ( $i<@{$recordset} ) {
  $recordset->[$i++]{id} =~ /000$/ and splice (@{$recordset},--$i,1);
}

-sln


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

Date: Sat, 7 Nov 2009 23:46:13 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: remove elements from array ref
Message-Id: <slrnhfc1m5.81o.nospam-abuse@powdermilk.math.berkeley.edu>

On 2009-11-07, sln@netherlands.com <sln@netherlands.com> wrote:
>
>               splice (@{$recordset},$i,1);

This is quadratic.  The usual linear-time idiom is to replace the
$i-th element by the (removed) last one.

Yours,
Ilya


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

Date: Sat, 07 Nov 2009 16:09:10 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: remove elements from array ref
Message-Id: <H_nJm.15339$Wf2.569@newsfe23.iad>

sln@netherlands.com wrote:
> On Sun, 8 Nov 2009 08:05:07 +1100, Martien Verbruggen <martien.verbruggen@invalid.see.sig> wrote:
> 
>> On Sat, 07 Nov 2009 12:37:25 -0800,
>> 	sln@netherlands.com <sln@netherlands.com> wrote:
>>> On Sat, 07 Nov 2009 14:47:41 -0500, monkeys paw <user@example.net> wrote:
>>>
>>>> sub _exclude_special_session {
>>>>     my $recordset = shift;
>>>>     my $i;
>>>>     for my $record (@{$recordset}) {
>>>>         if ($record->{id} =~ /000$/) {
>>>               splice (@{$recordset},$i,1);
>>>
>>>>         }
>>>>         $i++;
>>>>     }
>>From the perlsyn documentation:
>>       If any part of LIST is an array, "foreach" will get very confused if
>>       you add or remove elements within the loop body, for example with
>>       "splice".   So don’t do that.
>>
>> Martien
> 
> Oh, I didn't see the LIST context.
> Either one of these then.
> 
> for ( my $i=0; $i<@{$recordset}; $i++ ) {
>   $recordset->[$i]{id} =~ /000$/ and splice (@{$recordset},$i--,1);
> }
> 
> my $i = 0;
> while ( $i<@{$recordset} ) {
>   $recordset->[$i++]{id} =~ /000$/ and splice (@{$recordset},--$i,1);
> }

Better to just reverse the list:

for ( my $i = $#$recordset; $i >= 0; --$i ) {
   $recordset->[ $i ]{ id } =~ /000$/ and splice @$recordset, $i, 1;
}


for my $i ( reverse 0 .. $#$recordset ) {
   $recordset->[ $i ]{ id } =~ /000$/ and splice @$recordset, $i, 1;
}




John
-- 
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway


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

Date: Sat, 07 Nov 2009 16:14:53 -0800
From: sln@netherlands.com
Subject: Re: remove elements from array ref
Message-Id: <no2cf5l35nh9a372vqrvhgrgdck6qblmld@4ax.com>

On Sat, 7 Nov 2009 23:46:13 +0000 (UTC), Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:

>On 2009-11-07, sln@netherlands.com <sln@netherlands.com> wrote:
>>
>>               splice (@{$recordset},$i,1);
>
>This is quadratic.  The usual linear-time idiom is to replace the
>$i-th element by the (removed) last one.
>
>Yours,
>Ilya

Not sure what you mean by this. This form of splice shrinks the array
by what, something like a memmove() in a typless way.

The solution is to do your own itterating.

Shrink with no adjustment:
 for (@{$recordset}) {
    $recordset->[$i]{id} =~ /000$/ and splice (@{$recordset},$i,1);
 }

Shrink with adjustment:
  for ( my $i=0; $i<@{$recordset}; $i++ ) {
     $recordset->[$i]{id} =~ /000$/ and splice (@{$recordset},$i--,1);
  }

-sln


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

Date: Sat, 07 Nov 2009 16:18:31 -0800
From: sln@netherlands.com
Subject: Re: remove elements from array ref
Message-Id: <jg3cf55562s97jagvp9ifl09o5puc887pt@4ax.com>

On Sat, 07 Nov 2009 16:09:10 -0800, "John W. Krahn" <someone@example.com> wrote:

>sln@netherlands.com wrote:
>> On Sun, 8 Nov 2009 08:05:07 +1100, Martien Verbruggen <martien.verbruggen@invalid.see.sig> wrote:
>> 
>>> On Sat, 07 Nov 2009 12:37:25 -0800,
>>> 	sln@netherlands.com <sln@netherlands.com> wrote:
>>>> On Sat, 07 Nov 2009 14:47:41 -0500, monkeys paw <user@example.net> wrote:
>>>>
>>>>> sub _exclude_special_session {
>>>>>     my $recordset = shift;
>>>>>     my $i;
>>>>>     for my $record (@{$recordset}) {
>>>>>         if ($record->{id} =~ /000$/) {
>>>>               splice (@{$recordset},$i,1);
>>>>
>>>>>         }
>>>>>         $i++;
>>>>>     }
>>>From the perlsyn documentation:
>>>       If any part of LIST is an array, "foreach" will get very confused if
>>>       you add or remove elements within the loop body, for example with
>>>       "splice".   So don’t do that.
>>>
>>> Martien
>> 
>> Oh, I didn't see the LIST context.
>> Either one of these then.
>> 
>> for ( my $i=0; $i<@{$recordset}; $i++ ) {
>>   $recordset->[$i]{id} =~ /000$/ and splice (@{$recordset},$i--,1);
>> }
>> 
>> my $i = 0;
>> while ( $i<@{$recordset} ) {
>>   $recordset->[$i++]{id} =~ /000$/ and splice (@{$recordset},--$i,1);
>> }
>
>Better to just reverse the list:
>
>for ( my $i = $#$recordset; $i >= 0; --$i ) {
>   $recordset->[ $i ]{ id } =~ /000$/ and splice @$recordset, $i, 1;
>}
>
>
>for my $i ( reverse 0 .. $#$recordset ) {
>   $recordset->[ $i ]{ id } =~ /000$/ and splice @$recordset, $i, 1;
>}
>
>
>
>
>John

Yes, much better to start at the end, don't have to move so much data.

-sln


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

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 2667
***************************************


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