Recursive indenting of lines in a string?

Notices and updates
Locked
TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Recursive indenting of lines in a string?

Post by TedWalther »

(setq foo "a\nb\nc\n")
(indent foo "\t\t") => "\t\ta\n\t\tb\n\t\tc\n"

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 "\\n\t\t" 0) => "\\n\t\ta\\n\t\tb\\n\t\tc\\n\t\t"

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 \n\r, \r\n, 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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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:

Code: Select all

(dolist (l (parse foo "\\n|\\r" 0))
  (unless (= "" l) 
    (print "\t\t" 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
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Code: Select all

(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...

Artful code

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Post by rickyboy »

Hi Ted! This

Code: Select all

(replace "([^\r\n]+)([\r\n]+)" foo (string "\t\t" $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
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Post by TedWalther »

rickyboy wrote:Hi Ted! This

Code: Select all

(replace "([^\r\n]+)([\r\n]+)" foo (string "\t\t" $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
Last edited by TedWalther on Thu Oct 04, 2007 8:05 pm, edited 1 time in total.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

TedWalther wrote: 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 one day!

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Post by TedWalther »

cormullion wrote:
TedWalther wrote: 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 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

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Perl. However, the line will be about 300 characters long.
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

Locked