Spliting a line and passing the results

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

Sean H. wrote:

 if user = ldap.get_entry(username)

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.

Jamey C. [email protected] wrote:

id, username, phone, lots, more, variables =

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

:slight_smile:

robert

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!

On 11/9/06, Sean H. [email protected] wrote:

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))]

On 10.11.2006 04:45, Sean H. wrote:

On 11/9/06, Jan S. [email protected] wrote:

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

That’s fantastic. Thank you!

Sean

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

On 10.11.2006 17:14, Sean H. wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs