1. Tabulators.......

Hy,

Can someone help me with this....
It's something that converts "\t" to "        "
With "\tProgress\tBar\tTest\t..." it works, but not with
     "Progress\tBar\tTest\t..."
Very strange.
But it's ofcours a mathematical error.


[BOF]

function multimatch(sequence str,sequence cstr)
sequence temp1,temp2
object loc,pl
temp1=""
temp2=cstr
pl=0
while 1 do
 loc=match(str,temp2)
 if loc=0 then
  exit
 else
  pl=pl+1
  temp1=append(temp1," ")
  temp1[pl]=loc+((pl-1)*length(str))
  for a=1 to length(str) do
   temp2=temp2[1..loc-1] & temp2[loc+1..length(temp2)]
  end for
 end if
end while
return temp1
end function

global function convertTab(sequence text,integer tabsize)
object tmp1,tab,count
tab=""
count=0
tmp1=multimatch("\t",text)
tabsize+=1
for a=1 to tabsize do
 tab=tab&" "
end for
for a=1 to length(tmp1) do
 if tmp1[a]=(a-1)*tabsize then
  text=text[1..a-1+count] & tab[1..length(tab)] &
text[a+1+count..length(text)]
  count+=tabsize
 else
  for b=1 to tabsize do
   if tmp1[a]=(a-1)*tabsize+b+count then
    text=text[1..a-1+count] & tab[1..length(tab)-b] &
text[a+1+count..length(text)]
    count+=tabsize-b
   end if
  end for
 end if
end for
return text
end function

sequence text
text=convertTab("\tProgress\tBar\tTest\t...",8)

[EOF]

Bye,
PQ
QC
Get Your Private, Free Email at http://www.hotmail.com

new topic     » topic index » view message » categorize

2. Re: Tabulators.......

EU>Hy,

EU>Can someone help me with this....
EU>It's something that converts "\t" to "        "
EU>With "\tProgress\tBar\tTest\t..." it works, but not with
EU>     "Progress\tBar\tTest\t..."
EU>Very strange.
EU>But it's ofcours a mathematical error.

I don't know why it works, but this should work:

global function convertTab(sequence s, integer n)
    sequence r
    integer i
    i = 1
    r = ""
    while i <= length(s) do
        if s[i] = '\t' then
            r = r & repeat(' ',n)
        else
            r = r & s[i]
        end if
        i = i + 1
    end while
    return r
end function

Jeffrey Fielding
JJProg at cyberbury.net
http://members.tripod.com/~JJProg/

new topic     » goto parent     » topic index » view message » categorize

3. Re: Tabulators.......

On Thu, 11 Mar 1999, Patrick Quist wrote:

] Can someone help me with this....
] It's something that converts "\t" to "        "
] With "\tProgress\tBar\tTest\t..." it works, but not with
]      "Progress\tBar\tTest\t..."
] Very strange.
] But it's ofcours a mathematical error.

[Code snipped to save space. This post is long enough... See previous post
for Patrick's code.]

Multimatch is a bit confusing. From what I can tell, you're trying to
return all the positions within the first string that the second occurs.
However this is an ambiguous problem. What should it return if given:

    multimatch("aaaaaa", "aaa")?

Should it return {1,4} or {1,2,3,4}?

I understand in the case of your program, that the former is more
acceptable :)

Here's one of my infamous on-the-fly rewrites:

function multimatch(sequence sub, sequence main)
    -- Parameter order changed to match that of Euphoria's match()
    sequence matches
    integer pos, totalpos

    matches = {}
    totalpos = 0
    pos = match(sub, main)

    while pos do
        totalpos = totalpos + pos -- Remember where we are along 'main'
        matches = matches & totalpos
        pos = 0 -- We'd get some nasty errors without the 0 and 'if'
        if totalpos+length(sub) <= length(main) then
            pos = match(sub, main[totalpos+length(sub)..length(main)])
        end if
    end while

    return matches
end function

function convertTab(sequence str, integer tabsize)
    sequence tabs, spaces

    tabs = multimatch("\t", str)
    if not length(tabs) then
        return str
    end if

    spaces = repeat(' ', tabsize)

    for i = 1 to length(tabs) do
        str =
            str[1..tabs[i]-1] &
            spaces[1..tabsize-remainder(tabs[i]-1, tabsize)]
            str[tabs[i]+1..length(str)]
    end for

    return str
end function

HTH,
Carl

--
Carl R White -- Final Year Computer Science at the University of Bradford
E-mail........: cyrek- at -bigfoot.com -- Remove hyphens. Ta :)
URL...........: http://www.bigfoot.com/~cyrek/
Uncrackable...: "19.6A.23.38.52.73.45 25.31.1C 3C.53.44.39.58"

new topic     » goto parent     » topic index » view message » categorize

4. Re: Tabulators.......

>
>Multimatch is a bit confusing. From what I can tell, you're trying to
>return all the positions within the first string that the second
occurs.
>However this is an ambiguous problem. What should it return if given:
>
>    multimatch("aaaaaa", "aaa")?
>
>Should it return {1,4} or {1,2,3,4}?
>

Neither, it returns {} because the 1st argument is bigger thatn the 2nd
one.
You should ask multimatch("aaa","aaaaaa"), and then it returns {1,4}
(I think I change it to {"long","short"} lookes nicer)

But there was nothing wrong with my multimatch code,
(except mine was a bit long, but I like to do things difficult)

It's the convertTab() that's causing the trouble,

If I run  : convertTab("\tHello,\tI'm Crazy\t!!!",8)
it returns:"        Hello,  I'        Crazy !!!"
                              ^-- Where is the 'm' ?

(if you run on my code, it returns the same)

So your code is the same but shorter.

Bye,
PQ
QC
Get Your Private, Free Email at http://www.hotmail.com

new topic     » goto parent     » topic index » view message » categorize

5. Re: Tabulators.......

On Thu, 11 Mar 1999 03:18:02 PST, Patrick Quist <quistnet at HOTMAIL.COM>
wrote:

>Hy,
>
>Can someone help me with this....
>It's something that converts "\t" to "        "
>With "\tProgress\tBar\tTest\t..." it works, but not with
>     "Progress\tBar\tTest\t..."
>Very strange.
>But it's ofcours a mathematical error.
>

I know you're looking for the math error, but why not
just use:

function replace(sequence oldstr, sequence text, sequence newstr)
integer x
x = match(oldstr,text)
while x do
      text = text[1..x-1] & newstr & text[x+length(oldstr)..length(text)]
      x = match(oldstr,text)
end while
return text
end function

sequence text
text = "\tProgress\tBar\tTest..."
text = replace("\t",text,"       ")
puts(1,text)

<snip old code>

Irv

new topic     » goto parent     » topic index » view message » categorize

6. Re: Tabulators.......

On Thu, 11 Mar 1999, Patrick Quist wrote:

] >However this is an ambiguous problem. What should it return if given:
] >    multimatch("aaaaaa", "aaa")?
] >Should it return {1,4} or {1,2,3,4}?
]
] Neither, it returns {} because the 1st argument is bigger thatn the 2nd
] one.
] You should ask multimatch("aaa","aaaaaa"), and then it returns {1,4}

Hmm. I was already thinking ahead to my example. Sorry... :)

] ... there was nothing wrong with my multimatch code,
] (except mine was a bit long, but I like to do things difficult)

I have a habit of rewriting people's code to fit my personal style. It's
not because I don't like your code, it's because rewriting it helps me
(and maybe others) understand it better.

] It's the convertTab() that's causing the trouble,
] If I run  : convertTab("\tHello,\tI'm Crazy\t!!!",8)
] it returns:"        Hello,  I'        Crazy !!!"
]                               ^-- Where is the 'm' ?

Running my code mentally assuming multimatch is Ok (using dots to
represent tabbed spaces):
........Hello,

Ah-ha. Bug found. The tab positions returned by multimatch() no longer
apply to str. str has been made longer by adding spaces.
/me slaps forehead

Here's a rewritten convertTab():
function convertTab(sequence str, integer tabsize)
    sequence tabs, spaces
    integer howfar

    tabs = multimatch("\t", str)
    if not length(tabs) then
        return str
    end if

    spaces = repeat(' ', tabsize)

    for i = 1 to length(tabs) do
        howfar = tabsize - remainder(tabs[i]-1), tabsize)
        str =
            str[1..tabs[i]-1] &
            spaces[1..howfar]
            str[tabs[i]+1..length(str)]
        -- stretch 'tabs' to accomodate added spaces
        tabs[i..length(tabs)] = tabs[i..length(tabs)] + howfar
        -- if we don't need 'tabs' again we could really shorten this to:
        -- tabs = tabs + howfar -- or use += with 2.1
    end for

    return str
end function

See if this works :)
Carl

--
Carl R White -- Final Year Computer Science at the University of Bradford
E-mail........: cyrek- at -bigfoot.com -- Remove hyphens. Ta :)
URL...........: http://www.bigfoot.com/~cyrek/
Uncrackable...: "19.6A.23.38.52.73.45 25.31.1C 3C.53.44.39.58"

new topic     » goto parent     » topic index » view message » categorize

7. Re: Tabulators.......

>
>See if this works :)
>Carl
>

Hy,

The code you send was better, but this is it....
You had :
        str =
            str[1..tabs[i]-1] &
            spaces[1..howfar] &
            str[tabs[i]+1..length(str)]

But the +1 is not needed somehow...

It's perfect now,
With "\tHello,\tI'm Crazy\t!!!",8
it returns
"        Hello,  I'm Crazy       !!!"
 ^^^^^^^^      ^^         ^^^^^^^
(also with other examples it's very good)

8 spaces                = 1xtab
6 letters & 2 spaces    = 6+2 = 1xtab
9 letters & 7 spaces    = 9+7 = 16 = 2xtabs


Thanks for your help....


[BOF]

function multimatch(sequence sub, sequence main)
    -- Parameter order changed to match that of Euphoria's match()
    sequence matches
    integer pos, totalpos
    matches = {}
    totalpos = 0
    pos = match(sub, main)
    while pos do
        totalpos = totalpos + pos
        matches = matches & totalpos
        pos = 0
        if totalpos+length(sub) <= length(main) then
            pos = match(sub, main[totalpos+length(sub)..length(main)])
        end if
    end while
    return matches
end function

function convertTab(sequence str, integer tabsize)
    sequence tabs, spaces
    integer howfar
    tabs = multimatch("\t", str)
    if not length(tabs) then
        return str
    end if
    spaces = repeat(' ', tabsize)
    for i = 1 to length(tabs) do
        howfar = tabsize - remainder(tabs[i], tabsize)
        str =
            str[1..tabs[i]-1] &
            spaces[1..howfar] &
            str[tabs[i]..length(str)]
        tabs[i..length(tabs)] = tabs[i..length(tabs)] + howfar
    end for
    return str
end function


sequence text
text=convertTab("\tHello,\tI'm Crazy\t!!!",8)

puts(1,text)

[EOF]


Bye,
PQ
QC
Get Your Private, Free Email at http://www.hotmail.com

new topic     » goto parent     » topic index » view message » categorize

8. Re: Tabulators.......

Hy,

I normal DOS you don't the {9}'s
but when I traced I saw that
the {9}'s ("\t") still existed
and that the tabs are 1 to short
So I added this to change the 9's to 32's

function deleteTabSign(sequence str)
 sequence tabs
 tabs = multimatch("\t", str)
 for a=1 to length(tabs) do
  str[a]=' '
 end for
return str
end function

Thanks again,
PQ
QC

Get Your Private, Free Email at http://www.hotmail.com

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu