in reply to Nested list items

You need to write a parser.

state = [] ARGF.each do |line| state.push :input if line =~ /<input>/ state.push :ordered if line =~ /<ordered-list>/ state.push :bulleted if line =~ /<bulleted-list>/ state.pop if line =~ /<end-list>/ or line =~ /<\/input>/ if line =~ /<item>/ case state.last when :ordered line.gsub!( /<item>/, "<ordered-item>" ) when :bulleted line.gsub!( /<item>/, "<bulleted-item>" ) else ## do nothing end end puts line end

Updated: Tweaked to actually run . . . and again to tweak state transitions to look prettier.

Replies are listed 'Best First'.
Re^2: Nested list items
by ikegami (Patriarch) on Sep 21, 2006 at 16:02 UTC

    Here's the Perl5 implementation of Fletch's code.

    my @state; while (<>) { push @state, 'input' if /<input>/; push @state, 'ordered' if /<ordered-list>/; push @state, 'bulleted' if /<bulleted-list>/; pop @state if /<end-list>/ || /<\/input>/; my $state = $state[$#state]; s/<item>/<ordered-item>/g if $state eq 'ordered'; s/<item>/<bulleted-item>/g if $state eq 'bulleted'; print; }

    Note that it has problems handling lines with multiple tags.

Re^2: Nested list items
by jdtoronto (Prior) on Sep 21, 2006 at 16:24 UTC
    Looks nice, but what the heck language is it?