By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,372 Members | 1,950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,372 IT Pros & Developers. It's quick & easy.

Preserving Array Values Positions

P: 30
Hello:

I have two lists of emails saved in flat files one email per line.

My script removes the emails in list 2 if they are present in list 1. Then reprints the email list, one email per line, back into list 2 but without the weeded out emails.

Except that when reprinting back to list 2 the order is random and I want to preserve the same order in which the emails were at first.

EXAMPLE:
if in email list 1 I have:

email_02@server.com
email_04@server.com
email_06@server.com
email_08@server.com
email_10@server.com

and if in email list 2 I have:

email_01@server.com
email_02@server.com
email_03@server.com
email_04@server.com
email_05@server.com
email_06@server.com
email_07@server.com
email_08@server.com
email_09@server.com
email_10@server.com

I want to end up with:

email_01@server.com
email_03@server.com
email_05@server.com
email_07@server.com
email_09@server.com

But Im getting :

email_09@server.com
email_03@server.com
email_07@server.com
email_05@server.com
email_01@server.com

Here is my script:

Expand|Select|Wrap|Line Numbers
  1. foreach (@All_Emails_1) {
  2. chomp;
  3.  
  4.    if( /\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}/ ){
  5.        $valid_emails{$_} = '1';
  6.  
  7.    }
  8.    }
  9. foreach(@All_Emails_2){
  10.     chomp;
  11.     if( $valid_emails{$_} eq '1' ){
  12. print"<B>($valid_emails{$_}) ($_)<br>";        
  13.         delete($valid_emails{$_});
  14.         push @repeated_emails,$_;
  15.     }
  16. }
  17. my @New_All_Emails_1 = keys(%valid_emails);
  18.  
  19.  
  20. $Back_Up_Emails_1 = 'All_Emails_BKUP.txt';
  21. $Old_All_Emails_1 = 'All_Emails_1.txt';
  22. $New_All_Emails_1 = 'All_Emails_1.txt';
  23.  
  24. rename($Old_All_Emails_1, $Back_Up_Emails_1)|| die print"Cant rename $Back_Up_Emails_1";
  25.  
  26. open (EMAILLIST, ">>$New_All_Emails_1") || die print"<br><b>Error: Cant open $New_All_Emails_1";
  27. flock (EMAILLIST);
  28. foreach $email @New_All_Emails_1){
  29. print EMAILLIST "$email\n";
  30. }
  31. flock (EMAILLIST, 8);
  32. close (EMAILLIST);    
Thanx for your help
virtualweb
Nov 11 '09 #1
Share this Question
Share on Google+
5 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
I guess my suggestion would be to do a sort:

Expand|Select|Wrap|Line Numbers
  1. foreach $email (sort(@New_All_Emails_1)){
  2.     print EMAILLIST "$email\n";
  3. }
  4.  
I haven't tested it, but that should do what you want. Let us know if it works.

Regards,

Jeff
Nov 12 '09 #2

P: 30
Hello numberwhun :

Thanx for your quick response.

I thought sort was used to arrange a list in a particular order. Alphabetically or from smaller to higher.

For ease of explanation I used the numerical emails 1 through 10, But emails surelly be very different in real life, and if I have emails in the follwing list:

zebra@server.com
young@server.com
xylophone@server.com
anthony@server.com

and suppose young@server.com gets weeded out, sort would arrange my list like this:

anthony@server.com
xylophone@server.com
zebra@server.com

and what I need is this: (what you might call a first comes first serve situation)

zebra@server.com
xylophone@server.com
anthony@server.com

My guess is that I need to assign a number to each email prior to weeding out the repeated emails and then as you say sort the numbers and just print back the emails in an ascendent order .. but I am not good working with hashes yet.
Nov 12 '09 #3

P: 33
Hi,

Please try this code.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. my (@list1, @list2, @new_list2);
  3. # read from flat files
  4. # assume @list1 is from list1.txt
  5. open (LIST1, "list1.txt");
  6. @list1 = <LIST1>;
  7. close(LIST1);
  8.  
  9. # assume @list2 is from list2.txt
  10. open (LIST2, "list2.xt");
  11. @list2 = <LIST2>;
  12. close(LIST2);
  13.  
  14. foreach my $email (@list2) {
  15.    my @exists = grep { $_ eq $email } @list1;
  16.    push @new_list2, $email unless @exists > 0;
  17. }
  18.  
  19. # write @new_list2 to list2.txt
  20. open(NEW_LIST2, ">list2.txt");
  21. foreach (@new_list2) {print NEW_LIST2 $_."\n";}
  22. close(NEW_LIST2);
Regards,
Mohan
Nov 12 '09 #4

Expert Mod 100+
P: 589
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6.  
  7. my %list1;
  8.  
  9. open my $ilst1, '<', 'list1.txt' or die $!;
  10. while (<$list1>) {
  11.     chomp;
  12.     $list1{$_}++;
  13. }
  14. close $list1;
  15.  
  16. my @filtered;
  17. open my $list2, '<', 'list2.txt' or die $!;
  18. while (<$list2>) {
  19.     chomp;
  20.     push(@filtered, $_) unless exists $list1{$_};
  21. }
  22. close $list2;
  23.  
  24. print Dumper \@filtered;
Nov 12 '09 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
For your issue with hashes, try this reference.

Regards,

Jeff
Nov 13 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.