For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > delete function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author delete function
Irfan Sayed

2006-11-29, 3:57 am

Hi,

I have two arrays . one array contains the element which needs to be
deleted from the secound array in which that element is already present.


I have tried using delete function but somehow i did not succeed.

Could somebody please help me on this

Regards
Irfan.


Mug

2006-11-29, 3:57 am

Sayed, Irfan (Irfan) wrote:
> Hi,
>
> I have two arrays . one array contains the element which needs to be
> deleted from the secound array in which that element is already present.
>
>
> I have tried using delete function but somehow i did not succeed.
>
> Could somebody please help me on this
>
> Regards
> Irfan.
>
>
>


my @A = qw/ A B C D E F /;
my @KILL = qw/ B C D/ ;

my %H ; $H{$_} = '' for @A;
delete $H{$_} for ( @KILL ) ;

@A = keys %H; # The order becomes random
print "@A";

Believe the list will have much better ideas...
Perhaps grep// is another option, but I have no idea about the speed.

HTH,
Mug



D. Bolliger

2006-11-29, 7:58 am

Sayed, Irfan (Irfan) am Mittwoch, 29. November 2006 08:34:

> I have two arrays . one array contains the element which needs to be
> deleted from the secound array in which that element is already present.
>
> I have tried using delete function but somehow i did not succeed.


Did you check how "somehow" it failed? You could have used Data::Dumper to see
the effect of delete $array[$i]. See also

perldoc -f delete

Here's one way to do it. remove() takes references to the array for speed.

The sub could be adapted, f.ex to return a modified copy of $targ instead of
changing it directly, and/or return the number of actually deleted elements,
and/or the elements that could not be deleted.

#!/usr/bin/perl
use strict; use warnings;

my @wanted=qw / a c /; # delete this...
my @target=qw / a b c d /; # ...from this

# deletes contents of second array from first
# $targ is directly changed. Preserves order.
#
sub remove {
my ($targ, $want)=@_;

ref($targ) eq 'ARRAY' and ref($want) eq 'ARRAY'
or die 'wrong usage'; # check arguments

my %tmp=map {$_=>1} @$want; # to make lookup faster

@$targ=grep { !exists $tmp{$_} } @$targ;
}

remove (\@target, \@wanted);

print @target, "\n";

__END_

There may be a more direct way.

Dani
Irfan Sayed

2006-11-29, 7:58 am

Hi Mug,

Thanks for your mail. I have attached my script for your reference.
I have updated my script as per your suggesion but still i am not
getting proper result.

Let me explain you what my script does.

I have one array called as @repl which contains some values. I have
taken another array called as @foo and greped @repl array for "pu_"
string and stored the output in @foo

Now i need to delete all values(strings) which contains "pu_" from @repl
array.

So can you please look into the script and let me know where i am wrong

Regards
Irfan.



_____

From: Mug [mailto:perl@reborn.org]
Sent: Wednesday, November 29, 2006 3:29 PM
To: Sayed, Irfan (Irfan)
Subject: Re: delete function



Hi Irfan,

You may reply to the list next time, so more people could help.

Sayed, Irfan (Irfan) wrote:


Hi Mug,

I did not get the meaning of following lines



This is the exact code. Have you tried to run it ? Or do you have any
code
and bring out to discuss ?



my %H ; $H{$_} = '' for @A;



This is to use the the elems in the array to becoming keys of a hash,
ie. %H
( this also quite a common technique to handling array by hash )



delete $H{$_} for ( @KILL ) ;



And then, use a loop to delete all the keys that you want to delete (
the elems
you want to delete ) ,



@A = keys %H; # The order becomes random print "@A";



And finally take back the rest of keys from Hash , %H and put back
to the original array @A .

Good luck,
Mug



Could you please give me exact code and explain how it works

I will be highly obliged if you do the same.

Regards
Irfan.


-----Original Message-----
From: Mug [mailto:perl@reborn.org]
Sent: Wednesday, November 29, 2006 2:07 PM
To: Sayed, Irfan (Irfan)
Cc: beginners@perl.org
Subject: Re: delete function

Sayed, Irfan (Irfan) wrote:


Hi,

I have two arrays . one array contains the element which
needs to be
deleted from the secound array in which that element is
already


present.



I have tried using delete function but somehow i did not
succeed.

Could somebody please help me on this

Regards
Irfan.









Irfan Sayed

2006-11-29, 7:58 am

Attachment got blocked .... Sending again

_____

From: Sayed, Irfan (Irfan)
Sent: Wednesday, November 29, 2006 4:35 PM
To: 'Mug'
Cc: 'beginners@perl.org'
Subject: RE: delete function


Hi Mug,

Thanks for your mail. I have attached my script for your reference.
I have updated my script as per your suggesion but still i am not
getting proper result.

Let me explain you what my script does.

I have one array called as @repl which contains some values. I have
taken another array called as @foo and greped @repl array for "pu_"
string and stored the output in @foo

Now i need to delete all values(strings) which contains "pu_" from @repl
array.

So can you please look into the script and let me know where i am wrong

Regards
Irfan.



_____

From: Mug [mailto:perl@reborn.org]
Sent: Wednesday, November 29, 2006 3:29 PM
To: Sayed, Irfan (Irfan)
Subject: Re: delete function



Hi Irfan,

You may reply to the list next time, so more people could help.

Sayed, Irfan (Irfan) wrote:


Hi Mug,

I did not get the meaning of following lines



This is the exact code. Have you tried to run it ? Or do you have any
code
and bring out to discuss ?



my %H ; $H{$_} = '' for @A;



This is to use the the elems in the array to becoming keys of a hash,
ie. %H
( this also quite a common technique to handling array by hash )



delete $H{$_} for ( @KILL ) ;



And then, use a loop to delete all the keys that you want to delete (
the elems
you want to delete ) ,



@A = keys %H; # The order becomes random print "@A";



And finally take back the rest of keys from Hash , %H and put back
to the original array @A .

Good luck,
Mug



Could you please give me exact code and explain how it works

I will be highly obliged if you do the same.

Regards
Irfan.


-----Original Message-----
From: Mug [mailto:perl@reborn.org]
Sent: Wednesday, November 29, 2006 2:07 PM
To: Sayed, Irfan (Irfan)
Cc: beginners@perl.org
Subject: Re: delete function

Sayed, Irfan (Irfan) wrote:


Hi,

I have two arrays . one array contains the element which
needs to be
deleted from the secound array in which that element is
already


present.



I have tried using delete function but somehow i did not
succeed.

Could somebody please help me on this

Regards
Irfan.









nobull67@gmail.com

2006-11-29, 7:58 am

On Nov 29, 10:45 am, i...@dbolliger.ch (D. Bolliger) wrote:
> Sayed, Irfan (Irfan) am Mittwoch, 29. November 2006 08:34:
>
>
[color=darkred]
> You could have used Data::Dumper to see the effect of delete $array[$i].


Actually you can't. It looks like it has the same effect as
undef($array[$i]) but actually it does not (at least not quite). The
difference is not usually significant but in those cases where it is
not significant it is conventional to use undef() rather than delete().

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @a = ( 1 .. 5 );
undef $a[1];
delete $a[3];

print Dumper \@a;
__END__
$VAR1 = [
1,
undef,
3,
undef,
5
];


> See also
> perldoc -f delete


Yes, do.

nobull67@gmail.com

2006-11-29, 7:58 am



On Nov 29, 7:34 am, isa...@avaya.com (Irfan Sayed) wrote:
> Hi,
>
> I have two arrays . one array contains the element which needs to be
> deleted from the secound array in which that element is already present.
>
> I have tried using delete function but somehow i did not succeed.
>
> Could somebody please help me on this


perldoc -f splice

nobull67@gmail.com

2006-11-29, 7:58 am



On Nov 29, 12:18 pm, "nobul...@gmail.com" <nobul...@gmail.com> wrote:
> On Nov 29, 7:34 am, isa...@avaya.com (Irfan Sayed) wrote:
>
>
>perldoc -f splice


BTW: if you'd done "perldoc -f delete" it would have told you that
delete() didn't do what you wanted and that you were looking for
splice.

Please, if you have a question about a particular
function/module/error/whatever please try looking at the documentation
of that function/module/error/whatever _before_ posting to a newsgroup
or mailing list. (This is not specific to Perl).

John W. Krahn

2006-11-29, 6:59 pm

Sayed, Irfan (Irfan) wrote:
> Hi Mug,


Hello,

> Thanks for your mail. I have attached my script for your reference.
> I have updated my script as per your suggesion but still i am not
> getting proper result.
>
> Let me explain you what my script does.
>
> I have one array called as @repl which contains some values. I have
> taken another array called as @foo and greped @repl array for "pu_"
> string and stored the output in @foo
>
> Now i need to delete all values(strings) which contains "pu_" from @repl
> array.


@repl = grep { !/pu_/i } @repl;


John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
nobull67@gmail.com

2006-11-29, 6:59 pm



On Nov 29, 4:28 pm, kra...@telus.net (John W. Krahn) wrote:[color=darkred]
> Sayed, Irfan (Irfan) wrote:
>

@repl = grep { !/pu_/i } @repl;

That also "deletes" values that contain "PU_" or "Pu_" or "pU_".

Of course it doesn't actually delete elements from @repl at all. It
replaces the _entire_ contents of the array @repl with a _copy_ of
the old contents but with some items omitted. It is rare that this
makes a difference but on those rare occasions that it matters...

for ( reverse 0 .. $#repl ) {
splice @repl, $_, 1 if $repl[$_] =~ /pu_/;
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com