Spliting a line and passing the results

S

Sean Hussey

Hi everone,

I'm not quite sure what the best way to go about this is. I have a
file with one user per line. Each line contains ID, username, phone,
etc. I split all these up into variables and then either create a new
user or update an existing user:

File.open("users.txt", "r") do |file|
file.each_line { |line|
id, username, phone, lots, more, variables = line.chomp.split(/\|/);
if user = ldap.get_entry(username)
# Existing user. Check for update.
user.update(I, hate, passing, all, of, the, variables, that,
were, just, split)
else
# New user. Create!
user.new(Same, here, see, what, I mean?)
end
}
end

Should I put it all into an array? Hash? Would that make the split
line huge but save space on the method calls? How would you go about
this?

Thank you!

Sean
 
J

Jamey Cribbs

Sean said:
Hi everone,

I'm not quite sure what the best way to go about this is. I have a
file with one user per line. Each line contains ID, username, phone,
etc. I split all these up into variables and then either create a new
user or update an existing user:

File.open("users.txt", "r") do |file|
file.each_line { |line|
id, username, phone, lots, more, variables = line.chomp.split(/\|/);
if user = ldap.get_entry(username)
# Existing user. Check for update.
user.update(I, hate, passing, all, of, the, variables, that,
were, just, split)
else
# New user. Create!
user.new(Same, here, see, what, I mean?)
end
}
end

Should I put it all into an array? Hash? Would that make the split
line huge but save space on the method calls? How would you go about
this?

Well, if #update and #new are looking for the variables in the same
order that they are in the text file, you could do:


File.open("users.txt", "r") do |file|
file.each_line { |line|
rec = line.chomp.split(/\|/);
if user = ldap.get_entry(rec[1])
# Existing user. Check for update.
user.update(*rec)
else
# New user. Create!
user.new(*rec)
end
}
end



Jamey

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.
 
R

Robert Klemme

Jamey Cribbs said:
Sean said:
Hi everone,

I'm not quite sure what the best way to go about this is. I have a
file with one user per line. Each line contains ID, username, phone,
etc. I split all these up into variables and then either create a
new user or update an existing user:

File.open("users.txt", "r") do |file|
file.each_line { |line|
id, username, phone, lots, more, variables =
line.chomp.split(/\|/); if user = ldap.get_entry(username)
# Existing user. Check for update.
user.update(I, hate, passing, all, of, the, variables, that,
were, just, split)
else
# New user. Create!
user.new(Same, here, see, what, I mean?)
end
}
end

Should I put it all into an array? Hash? Would that make the split
line huge but save space on the method calls? How would you go about
this?

Well, if #update and #new are looking for the variables in the same
order that they are in the text file, you could do:


File.open("users.txt", "r") do |file|
file.each_line { |line|
rec = line.chomp.split(/\|/);
if user = ldap.get_entry(rec[1])
# Existing user. Check for update.
user.update(*rec)
else
# New user. Create!
user.new(*rec)
end
}
end

Or even

File.open("users.txt", "r") do |file|
file.each_line { |line|
rec = line.chomp.split(/\|/)
user.send(ldap.get_entry(rec[1]) ? :update : :new, *rec)
}
end

:)

robert
 
S

Sean Hussey

Wow. Now THAT is what I'm talking about.

Thank you!

Jamey Cribbs said:
Sean said:
Hi everone,

I'm not quite sure what the best way to go about this is. I have a
file with one user per line. Each line contains ID, username, phone,
etc. I split all these up into variables and then either create a
new user or update an existing user:

File.open("users.txt", "r") do |file|
file.each_line { |line|
id, username, phone, lots, more, variables =
line.chomp.split(/\|/); if user = ldap.get_entry(username)
# Existing user. Check for update.
user.update(I, hate, passing, all, of, the, variables, that,
were, just, split)
else
# New user. Create!
user.new(Same, here, see, what, I mean?)
end
}
end

Should I put it all into an array? Hash? Would that make the split
line huge but save space on the method calls? How would you go about
this?

Well, if #update and #new are looking for the variables in the same
order that they are in the text file, you could do:


File.open("users.txt", "r") do |file|
file.each_line { |line|
rec = line.chomp.split(/\|/);
if user = ldap.get_entry(rec[1])
# Existing user. Check for update.
user.update(*rec)
else
# New user. Create!
user.new(*rec)
end
}
end

Or even

File.open("users.txt", "r") do |file|
file.each_line { |line|
rec = line.chomp.split(/\|/)
user.send(ldap.get_entry(rec[1]) ? :update : :new, *rec)
}
end

:)

robert
 
S

Sean Hussey

Actually, let me step back. This is really good, but what if I wanted
named parameters instead? Or is there a way to split a line and load
it into a hash and pass that?

The file doesn't have header information, but I know what the order of
the fields will be. I guess I could just write a method, pass the
line to it for parsing, and return a hash. Would that be about right?

Thanks again!

Sean

Wow. Now THAT is what I'm talking about.

Thank you!

Jamey Cribbs said:
Sean Hussey wrote:
Hi everone,

I'm not quite sure what the best way to go about this is. I have a
file with one user per line. Each line contains ID, username, phone,
etc. I split all these up into variables and then either create a
new user or update an existing user:

File.open("users.txt", "r") do |file|
file.each_line { |line|
id, username, phone, lots, more, variables =
line.chomp.split(/\|/); if user = ldap.get_entry(username)
# Existing user. Check for update.
user.update(I, hate, passing, all, of, the, variables, that,
were, just, split)
else
# New user. Create!
user.new(Same, here, see, what, I mean?)
end
}
end

Should I put it all into an array? Hash? Would that make the split
line huge but save space on the method calls? How would you go about
this?

Well, if #update and #new are looking for the variables in the same
order that they are in the text file, you could do:


File.open("users.txt", "r") do |file|
file.each_line { |line|
rec = line.chomp.split(/\|/);
if user = ldap.get_entry(rec[1])
# Existing user. Check for update.
user.update(*rec)
else
# New user. Create!
user.new(*rec)
end
}
end

Or even

File.open("users.txt", "r") do |file|
file.each_line { |line|
rec = line.chomp.split(/\|/)
user.send(ldap.get_entry(rec[1]) ? :update : :new, *rec)
}
end

:)

robert
 
J

Jan Svitok

Actually, let me step back. This is really good, but what if I wanted
named parameters instead? Or is there a way to split a line and load
it into a hash and pass that?

The file doesn't have header information, but I know what the order of
the fields will be. I guess I could just write a method, pass the
line to it for parsing, and return a hash. Would that be about right?

KEYS = %w[ name id phone color temperature dog password ]

data = line.chomp.split(...)
hash = Hash[*(KEYS.zip(data))]
 
S

Sean Hussey

That's fantastic. Thank you!

Sean

Actually, let me step back. This is really good, but what if I wanted
named parameters instead? Or is there a way to split a line and load
it into a hash and pass that?

The file doesn't have header information, but I know what the order of
the fields will be. I guess I could just write a method, pass the
line to it for parsing, and return a hash. Would that be about right?

KEYS = %w[ name id phone color temperature dog password ]

data = line.chomp.split(...)
hash = Hash[*(KEYS.zip(data))]
 
R

Robert Klemme

KEYS = %w[ name id phone color temperature dog password ]

data = line.chomp.split(...)
hash = Hash[*(KEYS.zip(data))]

I am not sure what you actually gain with this - unless of course your
new and update methods would need to be rewritten to accept a Hash
because all arguments are optional.

Kind regards

robert
 
S

Sean Hussey

I guess I'd just rather not depend on the order of the fields in the
file always being the same. If they change, I can change the
split(...), but I won't have to change new or update. (I may want to,
but I won't have to based on file format.)

Does that make sense? Or is that not a good enough reason? I'm
looking for any advice people might have on style.

Thanks!

Sean

KEYS = %w[ name id phone color temperature dog password ]

data = line.chomp.split(...)
hash = Hash[*(KEYS.zip(data))]

I am not sure what you actually gain with this - unless of course your
new and update methods would need to be rewritten to accept a Hash
because all arguments are optional.

Kind regards

robert
 
R

Robert Klemme

I guess I'd just rather not depend on the order of the fields in the
file always being the same. If they change, I can change the
split(...), but I won't have to change new or update. (I may want to,
but I won't have to based on file format.)

Does that make sense? Or is that not a good enough reason? I'm
looking for any advice people might have on style.

Not to me. The order of fields must be taken care of by the /parsing/
part. Changing method parameters or even making them depend on that
seems a very bad idea to me. If the order changes in the file, change
the code that reads them - in your case you just need to change the
order of variables for the assignment.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top