473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Preserving Array Values Positions

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
5 1770
numberwhun
3,509 Expert Mod 2GB
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
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
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
RonB
589 Expert Mod 512MB
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
3,509 Expert Mod 2GB
For your issue with hashes, try this reference.

Regards,

Jeff
Nov 13 '09 #6

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

Similar topics

7
by: Adam Hartshorne | last post by:
As a result of a graphics based algorihtms, I have a list of indices to a set of nodes. I want to efficiently identify any node indices that are stored multiple times in the array and the...
9
by: mps | last post by:
Suppose..... arr = Array("only","a","test"); ArrayVar = "arr"; alert(ArrayVar); This the simplyfied version of my problem.. (alerting 'undefined' instead of 'only'...)
6
by: Abby Lee | last post by:
I've created the following array and want to be able to process each indivisual object whenever my function is called. Is this best done with a "foreach"? I don't see a foreach in my javascript...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
1
by: BobRoyAce | last post by:
I have a class that has several Subs that do DB things, some of which require the same set of parameters to be passed to a stored procedure. One class has 12 parameters and part of code used to...
3
by: quiz123 | last post by:
Hi folks, I have a problem. my function call is void Rotate_n(int array, int arraySize,int n); //precondition: arraysize>=1 && n>=0 && n<=arraySize //post condition: Values in the arrary are...
2
by: Clive Green | last post by:
Hello peeps, I am using PHP 5.2.2 together with MP3_Id (a PEAR module for reading and writing MP3 tags). I have been using PHP on the command line (Mac OS X Unix shell, to be precise), and am...
1
by: Neil Chambers | last post by:
I'm currently using multidimensional arrays in PoSH by assigning values to index positions. I'm doing this because AFAIK there is no way to initialise an array simply with the 'number' of...
3
by: sophia.agnes | last post by:
Dear all, I was going through the book "C a software engineering approach by darnell & Margolis" there was a section named sign preserving vs value preserving it is as follows sign...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.