Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing array elements

Status
Not open for further replies.

AMiSM

Technical User
Jan 26, 2006
128
US
Hi, everybody!

I need to remove the elements of list number 2 from list number 1. Can somebody please tell why this doesn't work:

$outputFile = "C:\\Documents and Settings\\a\\Desktop\\Output.doc";

unlink ( $outputFile );
while ( -e $outputFile ) {}


open ( LIST1, "<", "D:\\ATLinks.doc" );
@list1 = <LIST1>;
close ( LIST1 );
$list1 = "@list1";

open ( LIST2, "<", "D:\\ATLog1.doc" );
@list2 = <LIST2>;
close ( LIST2 );
chomp ( @list2 );

for ( @list2 ) {
$list1 =~ s/$_//gms;
}

open ( FIL, ">>", $outputFile );
print FIL $list1;
close ( FIL );
 
Look into the array splice function

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I've seen that mentioned elsewhere, but I'm not clear on how it applies. I've seen how it can be used to overwrite one array over the other, from a specific point onward, but how do you use it to remove one array from another, especially when the overlap is fragmented?

Is this a drop-in solution, or do you incorporate it into an algorithm?

Please excuse my larval ignorance, here.
 
Try something like this:

my %list_1;
my %list_2; #contains the elements to remove from list 1

open (FILE1, "file1") or die $!;
while(<FILE1>){
chomp($_);
$list_1{$_}=1;
}
close(FILE1);

open (FILE2, "file2") or die $!;
while(<FILE2>){
chomp($_);
$list_2{$_}=1;
}
close(FILE1);


delete @list_1{keys %list_2};


print join("\n", keys %list_1);



 
Whoa!! That's works so cool!! I'll be looking at this for awhile. It doesn't quite make sense to me, especially 'delete'.

The '%' denotes a hash, right? I've read that you use hashes for this kind of thing, but hashes seem a bit archane to me, just yet. Onward and upward, I guess.

Thanks, man!
 

cfmartin, why is the hash 'list_1' accessed as an array in your code?

==> delete @list_1{keys %list_2};

Is it one of those context things? Does delete operate only on the keys this way, or something?
 
It's a hash slice which I believe is a list context. This is similar to $hash{'an_item'} being in a scalar context. The clue here is the curly brackets which let you know you are working with a hash.

You could write the same thing as follows keeping everything in a scalar context:

foreach my $invalid (keys %list_2){
delete ($list_1{$invalid});
}

 

So, essentially, to get an array from a hash,...

@some_array = @some_hash{ [some_statement_that_returns_an_array_context] };

correct?
 
Yes exactly. Your @some_array contains the values from your %some_hash slice.

Like this:

my %hash;
@hash{'A'..'Z'} = (1..26); #Same as %hash = (A=>1, B=>2, C=>3 ....)

my @list;
@list = @hash{ qw/A B D F/ };

#@list = (1, 2, 4, 6);

 

I think I understand why hashes are used for the two lists problem. Between the 'delete' and 'grep' functions, I'm sensing a way of thinking and doing things.
This is some very cool stuff! I'm liking Perl more and more!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top