Prev: How to POST Form Data?
Next: name correspondence
From: w_a_x_man on 5 Jul 2010 14:24 On Jul 5, 3:48 am, Rolf Pedersen <rolf...(a)gmail.com> wrote: > [Note: parts of this message were removed to make it a legal post.] > > Hi > > I have a file with the following format (example): > > Save Format v3.0(19990112) > @begin Libraries > "felles.pbl" ""; > @end; > @begin Objects > "n_cst_xml_utils.sru" "felles.pbl"; > "n_melding.sru" "felles.pbl"; > @end; > > The data in the two begin/end blocks are lists, which may be longer than > shown. > > I'd like to extract an array of the filenames (first quote) in the @begin > Objects ... @end; block. > For the example above this should return ["n_cst_xml_utils.sru", > "n_melding.sru"] > > My initial idea was to treat the whole thing as one long string, and extract > the part within the being-end-block by using regexp, converting the result > back to individual lines (split '\n') and doing array.map and regexp to > single out the name in the first quote on each line. > But I keep hitting the wall, especially with the first step in this > approach... :o( > > I know this should be easily done in a couple of lines of code, but I can't > get it right. > > Appreciate any help! > > Best regards, > Rolf puts DATA.read.scan(/^@begin Objects(.*?)^@end;/m).flatten. map{|s| s.strip.to_a}.flatten.map{|s| s.split(/"/)[1]} __END__ Save Format v3.0(19990112) @begin Libraries "felles.pbl" ""; @end; @begin Objects "n_cst_xml_utils.sru" "felles.pbl"; "n_melding.sru" "felles.pbl"; @end; @begin Libraries "felles.pbl" ""; @end; @begin Objects "n_cst_xml_utils.sru" "felles.pbl"; "n_melding.sru" "felles.pbl"; @end;
From: Rolf Pedersen on 7 Jul 2010 05:35
[Note: parts of this message were removed to make it a legal post.] Robert: The use of flip flop operator opened a new door for me. Didn't know of this before... And new knowledge is the best knowledge! :o) w_a_x_man: I can't believe I didn't think of the possibility to use a simple split instead of a scan to extract the filenames between the first two quotation marks! Thanks to all for the great input I've gotten on this issue! :o) Best regards, Rolf On Mon, Jul 5, 2010 at 8:25 PM, w_a_x_man <w_a_x_man(a)yahoo.com> wrote: > On Jul 5, 3:48 am, Rolf Pedersen <rolf...(a)gmail.com> wrote: > > [Note: parts of this message were removed to make it a legal post.] > > > > Hi > > > > I have a file with the following format (example): > > > > Save Format v3.0(19990112) > > @begin Libraries > > "felles.pbl" ""; > > @end; > > @begin Objects > > "n_cst_xml_utils.sru" "felles.pbl"; > > "n_melding.sru" "felles.pbl"; > > @end; > > > > The data in the two begin/end blocks are lists, which may be longer than > > shown. > > > > I'd like to extract an array of the filenames (first quote) in the @begin > > Objects ... @end; block. > > For the example above this should return ["n_cst_xml_utils.sru", > > "n_melding.sru"] > > > > My initial idea was to treat the whole thing as one long string, and > extract > > the part within the being-end-block by using regexp, converting the > result > > back to individual lines (split '\n') and doing array.map and regexp to > > single out the name in the first quote on each line. > > But I keep hitting the wall, especially with the first step in this > > approach... :o( > > > > I know this should be easily done in a couple of lines of code, but I > can't > > get it right. > > > > Appreciate any help! > > > > Best regards, > > Rolf > > puts DATA.read.scan(/^@begin Objects(.*?)^@end;/m).flatten. > map{|s| s.strip.to_a}.flatten.map{|s| s.split(/"/)[1]} > > __END__ > Save Format v3.0(19990112) > @begin Libraries > "felles.pbl" ""; > @end; > @begin Objects > "n_cst_xml_utils.sru" "felles.pbl"; > "n_melding.sru" "felles.pbl"; > @end; > @begin Libraries > "felles.pbl" ""; > @end; > @begin Objects > "n_cst_xml_utils.sru" "felles.pbl"; > "n_melding.sru" "felles.pbl"; > @end; > > > |