Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Spliting a line and passing the results

Reply
Thread Tools

Spliting a line and passing the results

 
 
Sean Hussey
Guest
Posts: n/a
 
      11-09-2006
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

 
Reply With Quote
 
 
 
 
Jamey Cribbs
Guest
Posts: n/a
 
      11-09-2006
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



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.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-09-2006
Jamey Cribbs <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Sean Hussey
Guest
Posts: n/a
 
      11-09-2006
Wow. Now THAT is what I'm talking about.

Thank you!

On 11/9/06, Robert Klemme <(E-Mail Removed)> wrote:
> Jamey Cribbs <(E-Mail Removed)> wrote:
> > 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
>
>
>


 
Reply With Quote
 
Sean Hussey
Guest
Posts: n/a
 
      11-09-2006
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

On 11/9/06, Sean Hussey <(E-Mail Removed)> wrote:
> Wow. Now THAT is what I'm talking about.
>
> Thank you!
>
> On 11/9/06, Robert Klemme <(E-Mail Removed)> wrote:
> > Jamey Cribbs <(E-Mail Removed)> wrote:
> > > 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
> >
> >
> >

>
>


 
Reply With Quote
 
Jan Svitok
Guest
Posts: n/a
 
      11-09-2006
On 11/9/06, Sean Hussey <(E-Mail Removed)> 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))]

 
Reply With Quote
 
Sean Hussey
Guest
Posts: n/a
 
      11-10-2006
That's fantastic. Thank you!

Sean

On 11/9/06, Jan Svitok <(E-Mail Removed)> wrote:
> On 11/9/06, Sean Hussey <(E-Mail Removed)> 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))]
>
>


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-10-2006
On 10.11.2006 04:45, Sean Hussey wrote:
> On 11/9/06, Jan Svitok <(E-Mail Removed)> 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
 
Reply With Quote
 
Sean Hussey
Guest
Posts: n/a
 
      11-10-2006
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 11/10/06, Robert Klemme <(E-Mail Removed)> wrote:
> On 10.11.2006 04:45, Sean Hussey wrote:
> > On 11/9/06, Jan Svitok <(E-Mail Removed)> 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
>
>


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-10-2006
On 10.11.2006 17:14, Sean Hussey 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String spliting and inclusion Stuart Clarke Ruby 16 07-23-2009 09:13 AM
Spliting input at blank line Arfon Smith Ruby 3 09-26-2007 12:59 PM
Spliting dates and strings Denis Digital Photography 3 09-25-2007 02:03 PM
Spliting values and reversing a hash mlwollman@gmail.com Perl Misc 17 10-05-2006 05:53 AM
spliting string by one of several characters? NEWBIE jason@cyberpine.com ASP .Net 2 10-27-2003 10:25 PM



Advertisments