Recursive indenting of lines in a string?

Started by TedWalther, October 02, 2007, 06:22:05 PM

Previous topic - Next topic

TedWalther

(setq foo "anbncn")

(indent foo "tt") => "ttanttbnttcn"



Hi guys.  I've been fiddling with "replace" and using regex functionality, but not sure how to make it do what I want above.



I want the start of every line to have some indent characters inserted, but if it ends with a newline, I don't want indent characters inserted after the last newline.  And there isn't a newline at the beginning of the string, but I want the indent characters there.  This is as close as I made it:



(replace "(^|n)" foo "\ntt" 0) => "\ntta\nttb\nttc\ntt"



Can someone show me what is the right way to define the "indent" function?  And if it was robust for various values of newline, such as nr, rn, and r, that would be great too. Empty lines don't need any indent characters added, which may make it easier.



Also, would it make sense for this (indent) function to be part of the base install?  Also, I am doing (constant 'cat append) to save typing when I want to join a bunch of strings; could that be made standard in subsequent release of newlisp as well?



Ted
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

cormullion

#1
Hi Ted! My €0.02:



I'm sure you can do most things with regexen - you can do conditional lookaheads and stuff - it gets as complicated as you want, and then some.



As an alternative, why not write a little bit more code and spare your brain cells a bit. For example, split up your text, then process each line:


(dolist (l (parse foo "\n|\r" 0))
  (unless (= "" l)
    (print "tt" l "r")
    (print l)))


- you could push to a string instead of printing.



As for making things part of the newLISP core... I can't speak for Lutz, but generally things have to be pretty fundamental and generally useful for things to make it into the inner circle of core functions. Stuff that can be quickly whipped together as function or macro or defined in your init.lsp file tend to remain in the outer circle...

Jeff

#2
(replace "^" foo "t" 2)

That last int switch (2) sets it to be multiline, so the ^ anchor matches the beginning of the line rather than the beginning of the string.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

rickyboy

#3
Hi Ted!  This(replace "([^rn]+)([rn]+)" foo (string "tt" $1 $2) 4)
will meet all your requirements (if I read them correctly), including not putting tabs on blank lines.  Cheers, --Rick
(λx. x x) (λx. x x)

TedWalther

#4
Quote from: "rickyboy"Hi Ted!  This(replace "([^rn]+)([rn]+)" foo (string "tt" $1 $2) 4)
will meet all your requirements (if I read them correctly), including not putting tabs on blank lines.  Cheers, --Rick


Rick, thank you!  Just what I was wanting.



Cormullion, thank you for your solution as well.



Boy, newlisp is just the greatest new language I've come across in years.  It brings back all the fun I used to have with Commodore BASIC.  The fun of a Commodore 64, the power of Unix, the readability of Python.



Ted
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

cormullion

#5
Quote from: "TedWalther" The fun of a Commodore, the power of Unix, the readability of Python.


I don't know quite what that means, but perhaps it ought to go into http://www.alh.net/newlisp/phpbb/viewtopic.php?t=1128">//http://www.alh.net/newlisp/phpbb/viewtopic.php?t=1128 one day!

TedWalther

#6
Quote from: "cormullion"
Quote from: "TedWalther" The fun of a Commodore 64, the power of Unix, the readability of Python.


I don't know quite what that means, but perhaps it ought to go into http://www.alh.net/newlisp/phpbb/viewtopic.php?t=1128">//http://www.alh.net/newlisp/phpbb/viewtopic.php?t=1128 one day!


The Commodore 64 had sound and graphics, a floppy drive, and it worked with your TV set.  It was FUN.  It's built in BASIC interpreter let you peek and poke into the innards of the machine right from the beginning.  It hid nothing from you.



We all know Unix is powerful.  The first multi-tasking, multi-user, network enabled OS that ran on an inexpensive PC.



Python.  Python lends itself to writing readable code.  So does newLISP.  In what other language can someone hand me a one line "indent" function? WOW!



Ted
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

Jeff

#7
Perl.  However, the line will be about 300 characters long.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code