[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Spliting a line and passing the results

Sean Hussey

11/9/2006 7:22:00 PM

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

9 Answers

Jamey Cribbs

11/9/2006 7:40:00 PM

0

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.

Robert Klemme

11/9/2006 8:02:00 PM

0

Jamey Cribbs <jcribbs@netpromi.com> 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

Sean Hussey

11/9/2006 8:15:00 PM

0

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

Thank you!

On 11/9/06, Robert Klemme <shortcutter@googlemail.com> wrote:
> Jamey Cribbs <jcribbs@netpromi.com> 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
>
>
>

Sean Hussey

11/9/2006 9:58:00 PM

0

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 <seanhussey@gmail.com> wrote:
> Wow. Now THAT is what I'm talking about.
>
> Thank you!
>
> On 11/9/06, Robert Klemme <shortcutter@googlemail.com> wrote:
> > Jamey Cribbs <jcribbs@netpromi.com> 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
> >
> >
> >
>
>

Jano Svitok

11/9/2006 10:46:00 PM

0

On 11/9/06, Sean Hussey <seanhussey@gmail.com> 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))]

Sean Hussey

11/10/2006 3:46:00 AM

0

That's fantastic. Thank you!

Sean

On 11/9/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 11/9/06, Sean Hussey <seanhussey@gmail.com> 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))]
>
>

Robert Klemme

11/10/2006 9:14:00 AM

0

On 10.11.2006 04:45, Sean Hussey wrote:
> On 11/9/06, Jan Svitok <jan.svitok@gmail.com> 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

Sean Hussey

11/10/2006 4:14:00 PM

0

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 <shortcutter@googlemail.com> wrote:
> On 10.11.2006 04:45, Sean Hussey wrote:
> > On 11/9/06, Jan Svitok <jan.svitok@gmail.com> 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
>
>

Robert Klemme

11/10/2006 5:36:00 PM

0

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