one-liner for characater replacement
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.fortran Profile…
 Up
one-liner for characater replacement         


Author: analyst41
Date: May 18, 2008 12:03

I want to replace every occurrence of a dash in a string by a space.
Surely this can be done with a one-liner?

Thanks for any responses.
110 Comments
Re: one-liner for characater replacement         


Author: Sjouke Burry
Date: May 18, 2008 12:27

analyst41@hotmail.com wrote:
> I want to replace every occurrence of a dash in a string by a space.
> Surely this can be done with a one-liner?
>
> Thanks for any responses.
This MUST be from hotmail? Checking... YES!! it is.
no comments
Re: one-liner for characater replacement         


Author: James Giles
Date: May 18, 2008 12:36

analyst41@hotmail.com wrote:
> I want to replace every occurrence of a dash in a string by a space.
> Surely this can be done with a one-liner?

A very long and hard to read one-liner, sure. TRANSFER the string
to an array of character (you'll have to do this in two places: the TSOURCE
operand to MERGE, and inside the MASK expression to MERGE), use
MERGE (the FSOURCE operand is, of course, just a space character),
then use TRANSFER again to get the result back into the form of a string.

Unless the above is *very* much faster (or you can prove the code
using it is a *very* critical bottle-neck) you should prefer the more
legible simple scalar loop.

do i = 1, len(string)
if(string(i:i) == '-') String(i:i) = ' '
end do

Of course, you *can* make that a one liner:

do i = 1, len(string); if(string(i:i) == '-') String(i:i) = ' '; end do

--
J. Giles
Show full article (1.14Kb)
no comments
Re: one-liner for characater replacement         


Author: Terence
Date: May 18, 2008 16:59

(Cheating)
CALL NODASH(len,string)

I always write a tiny Fortran (or even assembler) routine and add it
to my library if there's a chance of needing it more that once.
no comments
Re: one-liner for characater replacement         


Author: James Giles
Date: May 18, 2008 18:04

Terence wrote:
> (Cheating)
> CALL NODASH(len,string)
>
> I always write a tiny Fortran (or even assembler) routine and add it
> to my library if there's a chance of needing it more that once.

Well, you shouldn't need to pass the length. The call should be

call replace(string, from, to)

Where FROM is a one character thing to be replaced and TO is
a one character thing to replace FROMs with.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
no comments
line numbers was : one-liner for characater replacement (was: one-liner for characater replacement)         


Author: Ron Ford
Date: May 18, 2008 20:57

On Sun, 18 May 2008 19:36:21 GMT, James Giles wrote:
> analyst41@hotmail.com wrote:
>> I want to replace every occurrence of a dash in a string by a space.
>> Surely this can be done with a one-liner?
>
> A very long and hard to read one-liner, sure. TRANSFER...
Show full article (2.24Kb)
no comments
Re: one-liner for characater replacement         


Author: David Frank
Date: May 19, 2008 02:01

hotmail.com> wrote in message
news:b35fe8c6-8cb4-49a4-955c-29343c54cbe3@b1g2000hsg.googlegroups.com...
>I want to replace every occurrence of a dash in a string by a space.
> Surely this can be done with a one-liner?
>
> Thanks for any responses.

Yes it can be a 1-liner.
Below ought to execute as fast, maybe faster since no explicit index
variable is invoked.
! ------------------
program test
integer,parameter :: ML=80 ! max length string processing
character(ML) :: s = 'replace-these-dashs-with-space-with-1-statement'
character :: a(ML) ; equivalence (a,s)

where (a == '-') a = ' ' ! manipulate s string
write (*,*) trim(s)
end program
no comments
Re: one-liner for characater replacement         


Author: Walter Spector
Date: May 19, 2008 05:49

analyst41@hotmail.com wrote:
> I want to replace every occurrence of a dash in a string by a space.
> Surely this can be done with a one-liner?

Of course.

program nodash
implicit none

character(20), parameter :: test_strings(3) = (/ &
"no-dashes-wanted----", &
"--------------------", &
"++++++++++++++++-+++" &
/)
character(20) :: results(3)

integer :: i,j

do, i=1, size (test_strings)

forall (j=1:len (test_strings)) &
results(i)(j:j) = merge (test_strings(i)(j:j), ' ', test_strings(i)(j:j) /= '-')

end do

print '(a/(a))', 'answers:', results

end program
Show full article (0.64Kb)
no comments
Re: one-liner for characater replacement         


Author: utab
Date: May 19, 2008 09:04

On May 19, 11:01 am, "David Frank" hotmail.com> wrote:
> hotmail.com> wrote in message
>
> news:b35fe8c6-8cb4-49a4-955c-29343c54cbe3@b1g2000hsg.googlegroups.com...
>
>>I want to replace every occurrence of a dash in a string by a space.
>> Surely this can be done with a one-liner?
>
>> Thanks for any responses.
>
> Yes it can be a 1-liner.
> Below ought to execute as fast, maybe faster since no explicit index
> variable is invoked.
> ! ------------------
> program test
> integer,parameter :: ML=80 ! max length string processing
> character(ML) :: s = 'replace-these-dashs-with-space-with-1-statement'
> character :: a(ML) ; equivalence (a,s)
Show full article (0.90Kb)
no comments
Re: one-liner for characater replacement         


Author: David Frank
Date: May 19, 2008 12:43

"utab" gmail.com> wrote in message
news:5c1a8340-24b6-43e0-9ca7-820138c64e31@24g2000hsh.googlegroups.com...
> On May 19, 11:01 am, "David Frank" hotmail.com> wrote:
>> hotmail.com> wrote in message
>>
>> news:b35fe8c6-8cb4-49a4-955c-29343c54cbe3@b1g2000hsg.googlegroups.com...
>>
>>>I want to replace every occurrence of a dash in a string by a space.
>>> Surely this can be done with a one-liner?
>>
>>> Thanks for any responses.
>>
>> Yes it can be a 1-liner.
>> Below ought to execute as fast, maybe faster since no explicit index
>> variable is invoked.
>> ! ------------------
>> program test
>> integer,parameter :: ML=80 ! max length string processing
>> character(ML) :: s = 'replace-these-dashs-with-space-with-1-statement'
>> character :: a(ML) ; equivalence (a,s) ...
Show full article (1.04Kb)
no comments
1 2 3 4 5 6 7 8 9