|
|
Up |
|
|
  |
Author: analyst41analyst41 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 |
|
  |
Author: Sjouke BurrySjouke Burry Date: May 18, 2008 12:27
> 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 |
|
  |
Author: James GilesJames Giles Date: May 18, 2008 12:36
> 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 |
|
  |
Author: TerenceTerence 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 |
|
  |
Author: James GilesJames 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 |
|
  |
Author: Ron FordRon Ford Date: May 18, 2008 20:57
On Sun, 18 May 2008 19:36:21 GMT, James Giles 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 |
|
  |
Author: David FrankDavid Frank Date: May 19, 2008 02:01
>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 |
|
  |
Author: Walter SpectorWalter Spector Date: May 19, 2008 05:49
> 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 |
|
  |
Author: utabutab Date: May 19, 2008 09:04
On May 19, 11:01 am, "David Frank" 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.
>
> 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 |
|
  |
|
|
  |
Author: David FrankDavid Frank Date: May 19, 2008 12:43
> On May 19, 11:01 am, "David Frank" 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.
>>
>> 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 |
|
|
|
|