Author Topic: Creating tables?  (Read 6791 times)

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Creating tables?
« on: October 17, 2006, 04:38:21 pm »
Hello,

SlickEdit 11.0.2.

I'd like to be able to create a 'table' from a tab-delimited text file.  That is, I'd like all the items in the second 'column' to line up, all the items in the 3rd column to line up, etc.

So, start with this,
AbstractKeyValueType     base:
AbstractOKSTRAHistObjektType     base: okstra:AbstractOKSTRAObjektType
AbstractOKSTRAObjektType     base: gml:AbstractFeatureType
AstType     base: okstra:AbstractOKSTRAHistObjektType

and end up with this:
AbstractKeyValueType                 base:
AbstractOKSTRAHistObjektType     base: okstra:AbstractOKSTRAObjektType
AbstractOKSTRAObjektType          base: gml:AbstractFeatureType
AstType                                    base: okstra:AbstractOKSTRAHistObjektType

i thought there was a way to do this, but maybe it's a CodeWright flashback...
TIA.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 4938
  • Hero Points: 404
Re: Creating tables?
« Reply #1 on: October 17, 2006, 04:49:19 pm »
Set the tab stops:  Document>Tabs...

Then there are various ways to convert the tab characters to spaces if you want like Edit>Other>Tabs to Spaces.

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Re: Creating tables?
« Reply #2 on: October 17, 2006, 05:29:12 pm »
Set the tab stops:  Document>Tabs...

Then there are various ways to convert the tab characters to spaces if you want like Edit>Other>Tabs to Spaces.

Thanks for the suggestion.

2 things:
-- "Edit->Other->Spaces to Tabs" (or space2tab) doesn't seem to work on spaces other than at the beginning of a line.  Or it seems to work erratically -- converting some spaces to tabs, but not others.
-- Merely replacing the spaces with tabs won't align the 'columns'.


hs2

  • Senior Community Member
  • Posts: 2738
  • Hero Points: 288
Re: Creating tables?
« Reply #3 on: October 17, 2006, 05:52:55 pm »
I think you need a macro for this ...
Here an extreeeemly stupid method, but it's not too complicated and maybe enough for a one-time job.
Code: [Select]
++cut++++++++++++
   _str line, p1='',p2='',p3='';

   get_line (line);
   parse line with p1 p2 p3;
   delete_line();
   p_col = 1;
   _insert_text( p1 );
   p_col = 25;
   _insert_text( p2 );
   p_col = 50;
   _insert_text( p3 );
--cut------------
(parse properly handles (mixed+mult.)spaces/tabs)

Again - that there are much better methods for such reformatting tasks (@see e.g.  Help -> Index -> Filtering marked text)

HS2

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 4938
  • Hero Points: 404
Re: Creating tables?
« Reply #4 on: October 17, 2006, 06:20:44 pm »
Type "save +e" on the SlickEdit command line to expand tabs.  Unfortunately, you'll need to reopen the file to get the results.  The Save As dialog doesn't provide the expand tabs feature of save.

Wanderer

  • Senior Community Member
  • Posts: 557
  • Hero Points: 23
Re: Creating tables?
« Reply #5 on: October 17, 2006, 06:34:39 pm »
Type "save +e" on the SlickEdit command line to expand tabs.  Unfortunately, you'll need to reopen the file to get the results.  The Save As dialog doesn't provide the expand tabs feature of save.

"View->Tab Chars" is checked.
'Indent with tabs' is turned off for .txt files.

This text file (jnk.txt) has no tabs in it:
word  gink
guget      ghost
flig    bink

Edit->Other->Spaces To Tabs doesn't do anything; the file isn't marked as changed, and no tabs are indicated.

If I use space2tab on the command line, I get this ('+' represents a tab, '-' is a virtual space):
word  gink
guget      ghost
flig+---bink

If I use 'save +e' and reopen the file there is no difference.




hs2

  • Senior Community Member
  • Posts: 2738
  • Hero Points: 288
Re: Creating tables?
« Reply #6 on: October 17, 2006, 07:15:48 pm »
try c/:b/\t/ on cmdline with Slick-regexp ON.
This converts all whitespaces (even mixed SPACE+TAB fields) to 1 TAB.

But I afraid that space<->tab conversion won't help you if you want to column align your file, right ?
The idea of the stupid example is to split a (or each) line into N separate parts and to re-insert them at a fix column.

But maybe I got you wrong ...

HS2


jbezem

  • Community Member
  • Posts: 84
  • Hero Points: 8
Re: Creating tables?
« Reply #7 on: October 18, 2006, 10:03:05 am »
I had the same needs occasionally, and I see two solutions:
First:
- Go through all relevant lines, and repeatedly use 'parse rest with start rest' until 'rest' is empty. For every iteration note the strlen of 'start' to find out the longest item in that position.
- set the tabstops to be further apart than the longest items in each position
- replace every whitespace with _one_ tab, therewith aligning all columns
- if required, use 'tab2space'

Or second:
- Replace all tabs with spaces
- on ech line, replace the first sequence of spaces with a (fixed) sequence large enough to make column one and the rest disjunct (column two will still have a ragged left side)
- go to the first element, in a column position one beyond the legth of the longest first column element, and start a block selection there, extending until the last line with a width to encompass the full ragged left side for all lines
- Use a regex to delete all spaces _within_ the selection before the second column, thereby aligning column two.
- Repeat for all columns

For a macro, the first method is easier and more foolproof, for manual execution the second method is easier. I currently do not have the time to develop this into a full-fledged macro, but since I occasionally need it myself, I might get round to it in the next few weeks, if still needed.

FWIW,

Johan

jbezem

  • Community Member
  • Posts: 84
  • Hero Points: 8
Re: Creating tables? Macro submitted...
« Reply #8 on: October 18, 2006, 11:10:07 am »
I couldn't leave my fingers from the keyboard in this, so I spent my lunch break.
Quick and dirty, but see if this helps you out; for production level code, you should save some settings, especially the tab settings for the buffer to be aligned, and one should be able to align just a selection within a file, but you can take it from here.
Code: [Select]
_command void createtable() name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
  _str line, start, rest;
  int maxvalues[];
  int curcol;

  /* Initialize */
  for (curcol = 0; curcol < 15; curcol++)
  {
    maxvalues[curcol] = 0;
  }

  /* For now, use the full buffer */
  top_of_buffer();
  do
  {
    get_line(line);
    rest = line;
    curcol = 0;
    while (rest != '')
    {
      parse rest with start rest;
      if (length(start) > maxvalues[curcol])
      {
        maxvalues[curcol] = length(start);
      }
      curcol++;
    }
  } while (0 == down());

  curcol = 0;
  tabstring = '';
  tabpos = 1;
  while (maxvalues[curcol] != 0)
  {
    tabpos = tabpos + maxvalues[curcol] + 1;
    tabstring = tabstring :+ tabpos :+ ' ';
    curcol++;
  }
  p_tabs = tabstring;
  top_of_buffer();
  begin_line();
  replace(':b', '\t', 'ER*');
  convert_tabs2spaces();
}
If you need more than 15 columns, just initialize more in the beginning of the command.

HTH,

Johan