Is there a more elegant way to do the following?
take the following string: 'file=TEST.DB;mode=644;create=false;' parse it, and return a Dictionary like so: 'file' -> 'TEST.DB', 'mode' -> 644, 'create' -> false This is what I came up with, but it feels kind of kludgy. I'm just wondering what other ways there are of accomplishing it? | connectionString settings tokens | connectionString := 'file=TEST.DB;mode=644;create=false;'. settings := Dictionary new. tokens := connectionString subStrings: ';'. 1 to: tokens size do: [ :ea | | option | option := (tokens at: ea) subStrings: '='. settings at: (option at: 1) put: (option at: 2). ]. |
Using single expression:
('file=TEST.DB;mode=644;create=false;' subStrings: ';') inject: Dictionary new into: [:dict :token | | option | option := token subStrings: '='. dict at: option first put: option second; yourself] Sean Malloy wrote: > Is there a more elegant way to do the following? > > take the following string: 'file=TEST.DB;mode=644;create=false;' > > parse it, and return a Dictionary like so: > > 'file' -> 'TEST.DB', > 'mode' -> 644, > 'create' -> false > > This is what I came up with, but it feels kind of kludgy. I'm just wondering > what other ways there are of accomplishing it? > > | connectionString settings tokens | > connectionString := 'file=TEST.DB;mode=644;create=false;'. > settings := Dictionary new. > tokens := connectionString subStrings: ';'. > 1 to: tokens size do: [ :ea | > | option | > option := (tokens at: ea) subStrings: '='. > settings at: (option at: 1) put: (option at: 2). > ]. > > > > |
> ('file=TEST.DB;mode=644;create=false;' subStrings: ';')
> inject: Dictionary new > into: [:dict :token | > | option | > option := token subStrings: '='. > dict at: option first put: option second; > yourself] ...And my twopence: don't forget to check option size. -- Dmitry Zamotkin |
In reply to this post by Sean Malloy-2
Sean Malloy wrote:
> Is there a more elegant way to do the following? > > take the following string: 'file=TEST.DB;mode=644;create=false;' > > parse it, and return a Dictionary like so: > > 'file' -> 'TEST.DB', > 'mode' -> 644, > 'create' -> false > > This is what I came up with, but it feels kind of kludgy. I'm just wondering > what other ways there are of accomplishing it? > > | connectionString settings tokens | > connectionString := 'file=TEST.DB;mode=644;create=false;'. > settings := Dictionary new. > tokens := connectionString subStrings: ';'. > 1 to: tokens size do: [ :ea | > | option | > option := (tokens at: ea) subStrings: '='. > settings at: (option at: 1) put: (option at: 2). > ]. (settings := Dictionary new) addAll: ( ( ( (connectionString subStrings: ';') collect: [:option| option subStrings: '='] ) select: [:valid| valid size = 2] ) collect: [:pair| pair first -> pair last] ) -cstb |
In reply to this post by Sean Malloy-2
Just to belabor the point: How about using a ReadStream?
rs := 'file=TEST.DB;mode=644;create=false;' readStream. args := Dictionary new. [rs atEnd] whileFalse: [ args at: (rs upTo: $=) put: (rs upTo: $;)]. args Don "Sean Malloy" <[hidden email]> wrote in message news:[hidden email]... > Is there a more elegant way to do the following? > > take the following string: 'file=TEST.DB;mode=644;create=false;' > > parse it, and return a Dictionary like so: > > 'file' -> 'TEST.DB', > 'mode' -> 644, > 'create' -> false > > This is what I came up with, but it feels kind of kludgy. I'm just > what other ways there are of accomplishing it? > > | connectionString settings tokens | > connectionString := 'file=TEST.DB;mode=644;create=false;'. > settings := Dictionary new. > tokens := connectionString subStrings: ';'. > 1 to: tokens size do: [ :ea | > | option | > option := (tokens at: ea) subStrings: '='. > settings at: (option at: 1) put: (option at: 2). > ]. > > > > |
You know what I love about smalltalk... So many different ways to achieve
the same task, I was trying think up the possible ways to do this in C#.. I guess my problems is I'm still not "Thinking" in Smalltalk. I only know a fairly limited part of the classes/methods, so its nice to see these very different ways of doing the same thing. The readStream version is so compact! The equivelent code in C# would be so horrible I wouldn't even think about doing it that way.... > ('file=TEST.DB;mode=644;create=false;' subStrings: ';') > inject: Dictionary new > into: [:dict :token | > | option | > option := token subStrings: '='. > dict at: option first put: option second; > yourself] > > (settings := Dictionary new) > addAll: > ( ( ( (connectionString subStrings: ';') > collect: [:option| option subStrings: '='] > ) select: [:valid| valid size = 2] > ) collect: [:pair| pair first -> pair last] > ) > > rs := 'file=TEST.DB;mode=644;create=false;' readStream. > args := Dictionary new. > [rs atEnd] whileFalse: [ > args at: (rs upTo: $=) put: (rs upTo: $;)]. > args |
In reply to this post by Don Rylander-3
You know what I love about smalltalk... So many different ways to achieve
the same task, I was trying think up the possible ways to do this in C#.. I guess my problems is I'm still not "Thinking" in Smalltalk. I only know a fairly limited part of the classes/methods, so its nice to see these very different ways of doing the same thing. The readStream version is so compact! The equivelent code in C# would be so horrible I wouldn't even think about doing it that way.... > ('file=TEST.DB;mode=644;create=false;' subStrings: ';') > inject: Dictionary new > into: [:dict :token | > | option | > option := token subStrings: '='. > dict at: option first put: option second; > yourself] > > (settings := Dictionary new) > addAll: > ( ( ( (connectionString subStrings: ';') > collect: [:option| option subStrings: '='] > ) select: [:valid| valid size = 2] > ) collect: [:pair| pair first -> pair last] > ) > > rs := 'file=TEST.DB;mode=644;create=false;' readStream. > args := Dictionary new. > [rs atEnd] whileFalse: [ > args at: (rs upTo: $=) put: (rs upTo: $;)]. > args |
Damned crappy outlook. Doesn't post if you don't have a mail account
configured properly...... Then it posted twice. Sorry! "Sean Malloy" <[hidden email]> wrote in message news:40c7872d$[hidden email]... > You know what I love about smalltalk... So many different ways to achieve > the same task, > > I was trying think up the possible ways to do this in C#.. I guess my > problems is I'm still not "Thinking" in Smalltalk. I only know a fairly > limited part of the classes/methods, so its nice to see these very different > ways of doing the same thing. The readStream version is so compact! The > equivelent code in C# would be so horrible I wouldn't even think about doing > it that way.... > > > > ('file=TEST.DB;mode=644;create=false;' subStrings: ';') > > inject: Dictionary new > > into: [:dict :token | > > | option | > > option := token subStrings: '='. > > dict at: option first put: option second; > > yourself] > > > > > > (settings := Dictionary new) > > addAll: > > ( ( ( (connectionString subStrings: ';') > > collect: [:option| option subStrings: '='] > > ) select: [:valid| valid size = 2] > > ) collect: [:pair| pair first -> pair last] > > ) > > > > > > rs := 'file=TEST.DB;mode=644;create=false;' readStream. > > args := Dictionary new. > > [rs atEnd] whileFalse: [ > > args at: (rs upTo: $=) put: (rs upTo: $;)]. > > args > > > > > |
Sean Malloy wrote:
> Damned crappy outlook. Doesn't post if you don't have a mail account > configured properly...... > > Then it posted twice. Sorry! Forgiven, this time :) You might consider http://www.mozilla.org/ The mail/news reader clones the nice parts of outlook, at least IMHO. The browser has image blocking abilities that I am really starting to enjoy. As new servers present themselves, just right click on the offending image, block images from the server, and hit refresh. It doesn't take too long to reduce clutter. Have a good one, Bill -- Wilhelm K. Schwab, Ph.D. [hidden email] |
> The mail/news reader clones the nice parts of outlook, at least IMHO.
> The browser has image blocking abilities that I am really starting to > enjoy. As new servers present themselves, just right click on the > offending image, block images from the server, and hit refresh. It > doesn't take too long to reduce clutter. I don't really need the mail part. Just news. I'll check out Thunderbird at home. Seems to be the mail and news part of mozilla stripped out into a seperate app. |
Free forum by Nabble | Edit this page |