Answered by:
How can I do this?

Question
-
I have some text stored in text file in format
email:password
email:password
email:password
I need to add all emails from that text file in some collection(arraylist or something) and add all password from that file in second collection(arraylist)..
How to do that...
ThanksWednesday, August 19, 2009 4:50 PM
Answers
-
Your suggested algorithm won't work the way you want it to work. Let's say we have this:
bob:Swordfish
jim:Alligator
frank:FriedFish
Now, when you do nested foreach statements like you're trying to do, you'll end up with the inner loop with the passwords going completely through the list before going on to the next email, which means, you'll send every email to the first person, then every email to the second person and so on. Given the data above, this would happen:
email sent to - password sent in email
bob - Swordfish
bob - Alligator
bob - Friedfish
jim - Swordfish
jim - Alligator
jim - Friedfish
frank - Swordfish
frank - Alligator
frank - Friedfish
Obviously, they don't all share their passwords.
The Dictionary<string, string> is still what you need. Watch how I've fleshed out the code below:
Dictionary<string, string> passwords = new Dictionary<string, string>();
foreach (string combo in File.ReadAllLines("file.txt"))
{
string[] values = combo.Split(':');
if (values.Length >= 2)
passwords.Add(values[0], values[1]);
}
foreach (KeyValuePair<string, string> kvp in passwords)
{
string email = kvp.Key;
string password = kvp.Value;
// send an email using the specified email and password
}
A dictionary will maintain the relationship between the email and the password, which is really what you want to do. I'm assuming you don't want to send out all the passwords to all the users.
Go look up Dictionary<TKey, TValue> on google. Click the first link.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • Twitter • LinkedIn • ForumsBrowser- Proposed as answer by Rudedog2 Wednesday, August 19, 2009 6:22 PM
- Edited by David M Morton Wednesday, August 19, 2009 6:30 PM
- Marked as answer by mbozan Wednesday, August 19, 2009 7:24 PM
Wednesday, August 19, 2009 5:25 PM -
Dictionary<string, string> passwords = new Dictionary<string, string>();
foreach (string combo in File.ReadAllLines("file.txt"))
{
string[] values = combo.Split(':');
if (values.Length >= 2 && !passwords.ContainsKey(values[0]))
passwords.Add(values[0], values[1]);
}
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • Twitter • LinkedIn • ForumsBrowser- Marked as answer by mbozan Wednesday, August 19, 2009 7:24 PM
Wednesday, August 19, 2009 7:02 PM -
one way:
foreach (KeyValuePair<string, string> kvp in accounts) { if(kvp.Key.Contains("gmail")) { //mail.... }
other way:
Dictionary<string, string> emails = new Dictionary<string, string>(); foreach (string combo in File.ReadAllLines(@"C:\User\Comp\Desktop\file.txt")) { string[] values = combo.Split(':'); if (values.Length >= 2) { // Filter here... for example... if (values[0].Containsh("gmail")) { // Now your emails will only be added if they match your criteria... emails.Add(values[0], values[1]); } }
- Marked as answer by mbozan Friday, August 21, 2009 1:01 AM
Friday, August 21, 2009 1:01 AM
All replies
-
Which part are you struggling with? Reading the file, or adding to a dictionary/arraylist.
Dictionary<TKey, TValue> is best for this:
Dictionary<string, string> passwords = new Dictionary<string, string>();
foreach (string combo in File.ReadAllLines("file.txt"))
{
string[] values = combo.Split(':');
if (values.Length >= 2)
passwords.Add(values[0], values[1]);
}
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • Twitter • LinkedIn • ForumsBrowserWednesday, August 19, 2009 4:54 PM -
I want to use two arraylist or something similar because I will later use
Arraylist emails=new Arraylist();
Arraylist passwords=new Arraylis();
foreach(string email in emails)
foreach(string password in passwords)
{
sending multiple emails...
}
I'm having problems with adding email from file to Arraylist "emails" and adding password to Arraylist "passwords"
I think that I need to separate strings and than filter them and save each to their arraylist...But I don't need how..
Any example..Wednesday, August 19, 2009 5:19 PM -
I want to use two arraylist or something similar because I will later use
Arraylist emails=new Arraylist();
Arraylist passwords=new Arraylis();
foreach(string email in emails)
foreach(string password in passwords)
{
sending multiple emails...
}
That ain't gonna work the you think. You'll be sending far more emails than you expect right now. Use the Debugger to step through your logic loops.
Mark the best replies as answers. "Fooling computers since 1971."Wednesday, August 19, 2009 5:25 PM -
Your suggested algorithm won't work the way you want it to work. Let's say we have this:
bob:Swordfish
jim:Alligator
frank:FriedFish
Now, when you do nested foreach statements like you're trying to do, you'll end up with the inner loop with the passwords going completely through the list before going on to the next email, which means, you'll send every email to the first person, then every email to the second person and so on. Given the data above, this would happen:
email sent to - password sent in email
bob - Swordfish
bob - Alligator
bob - Friedfish
jim - Swordfish
jim - Alligator
jim - Friedfish
frank - Swordfish
frank - Alligator
frank - Friedfish
Obviously, they don't all share their passwords.
The Dictionary<string, string> is still what you need. Watch how I've fleshed out the code below:
Dictionary<string, string> passwords = new Dictionary<string, string>();
foreach (string combo in File.ReadAllLines("file.txt"))
{
string[] values = combo.Split(':');
if (values.Length >= 2)
passwords.Add(values[0], values[1]);
}
foreach (KeyValuePair<string, string> kvp in passwords)
{
string email = kvp.Key;
string password = kvp.Value;
// send an email using the specified email and password
}
A dictionary will maintain the relationship between the email and the password, which is really what you want to do. I'm assuming you don't want to send out all the passwords to all the users.
Go look up Dictionary<TKey, TValue> on google. Click the first link.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • Twitter • LinkedIn • ForumsBrowser- Proposed as answer by Rudedog2 Wednesday, August 19, 2009 6:22 PM
- Edited by David M Morton Wednesday, August 19, 2009 6:30 PM
- Marked as answer by mbozan Wednesday, August 19, 2009 7:24 PM
Wednesday, August 19, 2009 5:25 PM -
I'll try this and get back to you,thanks for this,I didn't used Dictionary till now...Wednesday, August 19, 2009 6:18 PM
-
you have write in your code
string password = kvp.Password;
did you mean
string password = kvp.Value
because I get errorWednesday, August 19, 2009 6:29 PM -
you have write in your code
string password = kvp.Password;
did you mean
string password = kvp.Value
because I get error
I did. I've changed it for you.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • Twitter • LinkedIn • ForumsBrowserWednesday, August 19, 2009 6:30 PM -
it's working,,thanky you
I have one more questions
When there are duplicates email:password in Dictionary I get exception,how to remove duplicates..
How can I filter only for example Gmail emails to load it in Dictionary..Wednesday, August 19, 2009 6:57 PM -
Dictionary<string, string> passwords = new Dictionary<string, string>();
foreach (string combo in File.ReadAllLines("file.txt"))
{
string[] values = combo.Split(':');
if (values.Length >= 2 && !passwords.ContainsKey(values[0]))
passwords.Add(values[0], values[1]);
}
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • Twitter • LinkedIn • ForumsBrowser- Marked as answer by mbozan Wednesday, August 19, 2009 7:24 PM
Wednesday, August 19, 2009 7:02 PM -
how would you filter emails and password in Dictionary by provider(gmail,hotmail,yahoo)????Wednesday, August 19, 2009 8:26 PM
-
how would you filter emails and password in Dictionary by provider(gmail,hotmail,yahoo)????
Sorry man, this is a completely different question at this point. Start a new post.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • Twitter • LinkedIn • ForumsBrowserWednesday, August 19, 2009 8:27 PM -
one way:
foreach (KeyValuePair<string, string> kvp in accounts) { if(kvp.Key.Contains("gmail")) { //mail.... }
other way:
Dictionary<string, string> emails = new Dictionary<string, string>(); foreach (string combo in File.ReadAllLines(@"C:\User\Comp\Desktop\file.txt")) { string[] values = combo.Split(':'); if (values.Length >= 2) { // Filter here... for example... if (values[0].Containsh("gmail")) { // Now your emails will only be added if they match your criteria... emails.Add(values[0], values[1]); } }
- Marked as answer by mbozan Friday, August 21, 2009 1:01 AM
Friday, August 21, 2009 1:01 AM -
Your suggested algorithm won't work the way you want it to work. Let's say we have this:
bob:Swordfish
jim:Alligator
frank:FriedFish
Now, when you do nested foreach statements like you're trying to do, you'll end up with the inner loop with the passwords going completely through the list before going on to the next email, which means, you'll send every email to the first person, then every email to the second person and so on. Given the data above, this would happen:
email sent to - password sent in email
bob - Swordfish
bob - Alligator
bob - Friedfish
jim - Swordfish
jim - Alligator
jim - Friedfish
frank - Swordfish
frank - Alligator
frank - Friedfish
Obviously, they don't all share their passwords.
The Dictionary<string, string> is still what you need. Watch how I've fleshed out the code below:
Dictionary<string, string> passwords = new Dictionary<string, string>();
foreach (string combo in File.ReadAllLines("file.txt"))
{
string[] values = combo.Split(':');
if (values.Length >= 2)
passwords.Add(values[0], values[1]);
}
foreach (KeyValuePair<string, string> kvp in passwords)
{
string email = kvp.Key;
string password = kvp.Value;
// send an email using the specified email and password
}
A dictionary will maintain the relationship between the email and the password, which is really what you want to do. I'm assuming you don't want to send out all the passwords to all the users.
Go look up Dictionary<TKey, TValue> on google. Click the first link.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • Twitter • LinkedIn • ForumsBrowser
can I use instead of this:
foreach(GenericPair<TValue1, TValue2> kvp in passwords)
{
string email=kvp.Tvalue1; // or string email=kvp.first
string password=kvp.Tvalue2; // or string password=kvp.second
// send an email using the specified email and password
}Friday, October 9, 2009 12:55 AM -
can I use instead of this:
foreach (KeyValuePair<string, string> kvp in passwords)
{
string email = kvp.Key;
string password = kvp.Value;
// send an email using the specified email and password
}
//-------------------------------------------------------------------------------------------------
foreach(GenericPair<TValue1, TValue2> kvp in passwords)
{
string email=kvp.Tvalue1; // or string email=kvp.first
string password=kvp.Tvalue2; // or string password=kvp.second
// send an email using the specified email and password
}Friday, October 9, 2009 12:57 AM