From: "Basile Starynkevitch [news]" Date: 2005-05-19T06:25:13+09:00 Subject: Re: (newbie Q) opposite of inspect for strings On 2005-05-18, Thomas Adam wrote: (citing me, Basile S.) >> What is is opposite of inspect for strings, ie the function parsing >> strings external representation (as strings)? >> >> i.e. I do know that "a\tb".inspect gives the 6 character string >> "\"a\\tb\"" but what is the method or function that, given the >> argument "\"a\\tb\"" produces the "a\tb" string of 3 characters? > > I'll try this, assuming I understand you correctly. Sorry for having expressed myself poorly. I mean a="a\tb" binds variable a to a 3 character string (a, tab, b) aa=a.inspect binds variable aa to a 6 character string (dblquote, a, backslash, t, b, dblquote) I'm desperately seeking a function f such that b = f(aa) binds variable b to a 3 character string which is equal to the value of a, or I am seeking a method m such that c = aa.m binds variable c to a 3 character string (a, tab, b) equal to the value of a. The to_s method is not a valuable substitute for m since aa.to_s is a 6 character string (equal to aa) > >> Also how can I easily parse (ie read) from a file such a string like >> the output of inspect? > Actually I'm just trying to code in a quick and dirty way a ruby script dumping into a textual form a (rather small) GDBM file, whoses keys are all alphanumeric (and data are arbitrary binary strings). So far, I've managed to code the following script which seems to work. #! /usr/bin/ruby # $Id: gdbmdump 1 2005-05-18 20:45:15Z basile $ ## -*- ruby -*- require 'gdbm' srcdbmname=ARGV[0] destxtname=ARGV[1] STDERR.printf("start dumping gdbm %s into %s\n", srcdbmname, destxtname) if (not File.exists?(srcdbmname)) then STDERR.printf("source dbm %s does not exist\n", srcdbmname); exit(1) end keyarr=Array::new GDBM.open(srcdbmname, 0400) do |gdbm| nbk=0 gdbm.each do |key,val| if /^[a-zA-Z0-9+*._@,!(){}-]*$/ =~ key then keyarr << key nbk = nbk + 1 else STDERR.printf("bad key %s in file %s\n", key.inspect, srcdbmname); exit(1) end end STDERR.printf("got and sorting %d keys\n", nbk) keyarr.sort! if (File.exists?(destxtname)) then File.rename(destxtname,destxtname+"~") end File.open(destxtname, "w") do |out| keyarr.each do |key| val=gdbm[key] out.printf("%s\t%s\n", key, val.inspect) end end end STDERR.printf("end dumping gdbm %s into %s\n", srcdbmname, destxtname) ##eof $Id: gdbmdump 1 2005-05-18 20:45:15Z basile $ A typical output of the above script is ab "12" cd "45" where the file starts at the column a, and where keys and data are separated by a tabulation. My goal was to code the corresponding loading script gdbmload; the overall motivation for these 2 scripts is to manage under version control (Subversion) a GDBM file (by dumping it to & reloading it from a textual format, and by having a "canonical" dump format of it by sorting the keys; I don"t want to version control the GDBM binary file -because it is machine dependent- but a dump format of it, obtained thru this gdbmdump script and the opposite gdbmload script to be written) I am really confused and ashamed of asking such basic questions. My apologies to all, and a big thanks to Thomas Adam for having taken the time to answer them. Regards. -- Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basilestarynkevitchnet aliases: basiletunesorg = bstarynknerimnet 8, rue de la Fa�encerie, 92340 Bourg La Reine, France