String manipulation 
Author Message
 String manipulation

I am tring to strip the first 15 characters off of a string.  The
string has already had the function ADJUSTL applied to it so I can't
use string(1:15).  I am trying to keep it all on a single statement
because it is in an ELSE IF branch.  In other words I'd like to do
something like:

else if (adjustl(string)(1:15)) then  !  the 15 character subset is
applied to the
                                                 !  left justified
string

But of course the compiler thinks I am using an array rather than a
string subset with this usage.

Any suggestions?

Chris



Sun, 21 Aug 2011 01:27:25 GMT  
 String manipulation

Quote:

> I am tring to strip the first 15 characters off of a string.  The
> string has already had the function ADJUSTL applied to it so I can't
> use string(1:15).  I am trying to keep it all on a single statement
> because it is in an ELSE IF branch.  In other words I'd like to do
> something like:

> else if (adjustl(string)(1:15)) then  !  the 15 character subset is
> applied to the
>                                                  !  left justified
> string

Do you mean ?
  else if (adjustl(string)(1:15) == 'some characters') then


Sun, 21 Aug 2011 01:49:24 GMT  
 String manipulation

Quote:

> > I am tring to strip the first 15 characters off of a string. ?The
> > string has already had the function ADJUSTL applied to it so I can't
> > use string(1:15). ?I am trying to keep it all on a single statement
> > because it is in an ELSE IF branch. ?In other words I'd like to do
> > something like:

> > else if (adjustl(string)(1:15)) then ?! ?the 15 character subset is
> > applied to the
> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?! ?left justified
> > string

> Do you mean ?
> ? else if (adjustl(string)(1:15) == 'some characters') then

Yes.


Sun, 21 Aug 2011 02:20:09 GMT  
 String manipulation

Quote:

> I am tring to strip the first 15 characters off of a string.  The
> string has already had the function ADJUSTL applied to it so I can't
> use string(1:15).  I am trying to keep it all on a single statement
> because it is in an ELSE IF branch.  In other words I'd like to do
> something like:

> else if (adjustl(string)(1:15)) then  !  the 15 character subset is
> applied to the
>                                                  !  left justified
> string

[later corrected to add a test for the IF, but that is a separate
matter]

Quote:
> But of course the compiler thinks I am using an array rather than a
> string subset with this usage.

I'm not sure why the "of course". You can neither take a substring nor
an array slice of a function reference. Since both are invalid, I'm not
sure why it is "of course" that the compiler would interpret as one, but
not the other.

I can't see any reasonably simple way of expressing this on one line. I
suppose one could probably do it with suitable use of the verify
intrinsic (don't bother with the adjustl; just take the first 15
characters starting at the first non-blank, using verify to find the
first non-blank). But it would be pretty ugly - probably enough so that
even though it was in one statement, it wouldn't literally be one line,
though I suspect you actually meant "statement" instead of "line".
Perhaps something roughly like (off the top of my head)

  else if (string(verify(string,' '):verify(string,' ')+14)== &      
              'whatever') then

In addition to being pretty ugly, that lacks checks for things like
there being no non-blank characters. Adding such checks is probably
possible but would make it even uglier.

If it were my code, I'd probably do something like factor out at least
the adjustl to before the IF block. Depending on context, I suspect I
might factor out more than that (for example, if this is just checking
the first field in a string, I'd probably factor out the extraction of
that field leaving the test as just field=='whatever'; that's one I do
fairly often).

But if you want/need to keep it all in the "else if" statement, one
other option is to make a function, which you invoke from the else if.
That's away to "cheat" in that you can squeeze an arbitrary number of
statements into a function.

--
Richard Maine                    | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle           |  -- Mark Twain



Sun, 21 Aug 2011 03:02:42 GMT  
 String manipulation

Quote:

> > I am tring to strip the first 15 characters off of a string. ?The
> > string has already had the function ADJUSTL applied to it so I can't
> > use string(1:15). ?I am trying to keep it all on a single statement
> > because it is in an ELSE IF branch. ?In other words I'd like to do
> > something like:

> > else if (adjustl(string)(1:15)) then ?! ?the 15 character subset is
> > applied to the
> > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?! ?left justified
> > string

> [later corrected to add a test for the IF, but that is a separate
> matter]

> > But of course the compiler thinks I am using an array rather than a
> > string subset with this usage.

> I'm not sure why the "of course". You can neither take a substring nor
> an array slice of a function reference. Since both are invalid, I'm not
> sure why it is "of course" that the compiler would interpret as one, but
> not the other.

> I can't see any reasonably simple way of expressing this on one line. I
> suppose one could probably do it with suitable use of the verify
> intrinsic (don't bother with the adjustl; just take the first 15
> characters starting at the first non-blank, using verify to find the
> first non-blank). But it would be pretty ugly - probably enough so that
> even though it was in one statement, it wouldn't literally be one line,
> though I suspect you actually meant "statement" instead of "line".
> Perhaps something roughly like (off the top of my head)

> ? else if (string(verify(string,' '):verify(string,' ')+14)== & ? ? ?
> ? ? ? ? ? ? ? 'whatever') then

> In addition to being pretty ugly, that lacks checks for things like
> there being no non-blank characters. Adding such checks is probably
> possible but would make it even uglier.

> If it were my code, I'd probably do something like factor out at least
> the adjustl to before the IF block. Depending on context, I suspect I
> might factor out more than that (for example, if this is just checking
> the first field in a string, I'd probably factor out the extraction of
> that field leaving the test as just field=='whatever'; that's one I do
> fairly often).

> But if you want/need to keep it all in the "else if" statement, one
> other option is to make a function, which you invoke from the else if.
> That's away to "cheat" in that you can squeeze an arbitrary number of
> statements into a function.

> --
> Richard Maine ? ? ? ? ? ? ? ? ? ?| Good judgment comes from experience;
> email: last name at domain . net | experience comes from bad judgment.
> domain: summertriangle ? ? ? ? ? | ?-- Mark Twain

Thanks for your reply.

The "of course" is nothing other than "of course it didn't compile".

And yes the solutions that I came up with were "ugly" similar to your
example.  I was trying to keep it in the IF branch for clarity.  Since
I am a number type not a character manipulator, I was hoping somebody
had a slick way to handle this in the intrinsic language.  But as you
state I can always write a unique function.

Chris



Sun, 21 Aug 2011 03:13:27 GMT  
 String manipulation

Quote:



>>> I am tring to strip the first 15 characters off of a string.  The
>>> string has already had the function ADJUSTL applied to it so I can't
>>> use string(1:15).  I am trying to keep it all on a single statement
>>> because it is in an ELSE IF branch.  In other words I'd like to do
>>> something like:
>>> else if (adjustl(string)(1:15)) then  !  the 15 character subset is
>>> applied to the
>>>                                                  !  left justified
>>> string
>> [later corrected to add a test for the IF, but that is a separate
>> matter]

>>> But of course the compiler thinks I am using an array rather than a
>>> string subset with this usage.
>> I'm not sure why the "of course". You can neither take a substring nor
>> an array slice of a function reference. Since both are invalid, I'm not
>> sure why it is "of course" that the compiler would interpret as one, but
>> not the other.

>> I can't see any reasonably simple way of expressing this on one line. I
>> suppose one could probably do it with suitable use of the verify
>> intrinsic (don't bother with the adjustl; just take the first 15
>> characters starting at the first non-blank, using verify to find the
>> first non-blank). But it would be pretty ugly - probably enough so that
>> even though it was in one statement, it wouldn't literally be one line,
>> though I suspect you actually meant "statement" instead of "line".
>> Perhaps something roughly like (off the top of my head)

>>   else if (string(verify(string,' '):verify(string,' ')+14)== &      
>>               'whatever') then

>> In addition to being pretty ugly, that lacks checks for things like
>> there being no non-blank characters. Adding such checks is probably
>> possible but would make it even uglier.

>> If it were my code, I'd probably do something like factor out at least
>> the adjustl to before the IF block. Depending on context, I suspect I
>> might factor out more than that (for example, if this is just checking
>> the first field in a string, I'd probably factor out the extraction of
>> that field leaving the test as just field=='whatever'; that's one I do
>> fairly often).

>> But if you want/need to keep it all in the "else if" statement, one
>> other option is to make a function, which you invoke from the else if.
>> That's away to "cheat" in that you can squeeze an arbitrary number of
>> statements into a function.

>> --
>> Richard Maine                    | Good judgment comes from experience;
>> email: last name at domain . net | experience comes from bad judgment.
>> domain: summertriangle           |  -- Mark Twain

> Thanks for your reply.

> The "of course" is nothing other than "of course it didn't compile".

> And yes the solutions that I came up with were "ugly" similar to your
> example.  I was trying to keep it in the IF branch for clarity.  Since
> I am a number type not a character manipulator, I was hoping somebody
> had a slick way to handle this in the intrinsic language.  But as you
> state I can always write a unique function.

> Chris

Clarity and conciseness are not always compatible.


Sun, 21 Aug 2011 03:21:30 GMT  
 String manipulation

Quote:

> I am tring to strip the first 15 characters off of a string.

I don't get it!

If I wanted to strip 15 characters off a string line of, say 32,
characters, I would use

newstring=oldstring(16:32)
  and be done with it.

.



Sun, 21 Aug 2011 05:00:41 GMT  
 String manipulation

Quote:

> > I am tring to strip the first 15 characters off of a string.

> I don't get it!

> If I wanted to strip 15 characters off a string line of, say 32,
> characters, I would use

> newstring=oldstring(16:32)
> ? and be done with it.

> .

the adjustl function is used first to strip of the preceding spaces of
unknown length and then the next 15 characters stripped.


Sun, 21 Aug 2011 07:03:17 GMT  
 String manipulation

Quote:



>>> I am tring to strip the first 15 characters off of a string.
>> I don't get it!

>> If I wanted to strip 15 characters off a string line of, say 32,
>> characters, I would use

>> newstring=oldstring(16:32)
>>   and be done with it.

>> .

> the adjustl function is used first to strip of the preceding spaces of
> unknown length and then the next 15 characters stripped.

So, assuming you don't want to modify the original string, what about

   teststring = adjustl(string)
   if ( teststring(1:15) == ...some 15 char value... ) then
     ...do stuff...
   end if

?

cheers,

paulv



Sun, 21 Aug 2011 07:35:21 GMT  
 String manipulation

Quote:



> >>> I am tring to strip the first 15 characters off of a string.
> >> I don't get it!

> >> If I wanted to strip 15 characters off a string line of, say 32,
> >> characters, I would use

> >> newstring=oldstring(16:32)
> >> ? and be done with it.

> >> .

> > the adjustl function is used first to strip of the preceding spaces of
> > unknown length and then the next 15 characters stripped.

> So, assuming you don't want to modify the original string, what about

> ? ?teststring = adjustl(string)
> ? ?if ( teststring(1:15) == ...some 15 char value... ) then
> ? ? ?...do stuff...
> ? ?end if

> ?

> cheers,

> paulv- Hide quoted text -

> - Show quoted text -

Thanks,

unfortunately the IF block is fairly large and will not provide
clarity by declaring a separate dummy string outside the IF test.  I
will most likely end up writing a function to do what I want (where
having it contained in the IF test being the Most Important
Requirement).



Sun, 21 Aug 2011 08:10:38 GMT  
 String manipulation
...

Quote:
> the adjustl function is used first to strip of the preceding spaces of
> unknown length and then the next 15 characters stripped.

I don't really recommend it over either the factored version of
Richard's or the specific function, but

if(string(verify(string,' '):verify(string,' ')+15) == matchstring) then

would allow the one line test...

--



Sun, 21 Aug 2011 13:16:36 GMT  
 String manipulation

Quote:




> > >>> I am tring to strip the first 15 characters off of a string.
> > >> I don't get it!

> > >> If I wanted to strip 15 characters off a string line of, say 32,
> > >> characters, I would use

> > >> newstring=oldstring(16:32)
> > >> ? and be done with it.

> > >> .

> > > the adjustl function is used first to strip of the preceding spaces of
> > > unknown length and then the next 15 characters stripped.

> > So, assuming you don't want to modify the original string, what about

> > ? ?teststring = adjustl(string)
> > ? ?if ( teststring(1:15) == ...some 15 char value... ) then
> > ? ? ?...do stuff...
> > ? ?end if

> > ?

> > cheers,

> > paulv- Hide quoted text -

> > - Show quoted text -

> Thanks,

> unfortunately the IF block is fairly large and will not provide
> clarity by declaring a separate dummy string outside the IF test. ?I
> will most likely end up writing a function to do what I want (where
> having it contained in the IF test being the Most Important
> Requirement).- Tekst uit oorspronkelijk bericht niet weergeven -

> - Tekst uit oorspronkelijk bericht weergeven -

If you worry about this special function polluting the namespace,
then use an internal function:

subroutine character_crunching( ... )

   if ( my_special_function( string, other_string ) ) then
       ...
   endif

contains
logical function my_special_function( .... )
    ...
end function my_special_function
end subroutine character_crunching

(Works even if character_crunching is in a module!)

Regards,

Arjen



Sun, 21 Aug 2011 13:34:09 GMT  
 String manipulation

Quote:
> I am tring to strip the first 15 characters off of a string. ?The
> string has already had the function ADJUSTL applied to it so I can't
> use string(1:15). ?I am trying to keep it all on a single statement
> because it is in an ELSE IF branch. ?In other words I'd like to do
> something like:

> else if (adjustl(string)(1:15)) then ?! ?the 15 character subset is
> applied to the
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?! ?left justified
> string

> But of course the compiler thinks I am using an array rather than a
> string subset with this usage.

> Any suggestions?

> Chris

The kind of string matching you are after is known as "glob matching"
- like when you list all fortran source files (ls *.f90 or dir *.f90)
in a directory.

You can use one of the modules from my Flibs project for that -
http://flibs.sf.net.

Regards,

Arjen



Sun, 21 Aug 2011 16:03:56 GMT  
 String manipulation


Quote:


> > > I am tring to strip the first 15 characters off of a string. The
> > > string has already had the function ADJUSTL applied to it so I can't
> > > use string(1:15). I am trying to keep it all on a single statement
> > > because it is in an ELSE IF branch. In other words I'd like to do
> > > something like:

> > > else if (adjustl(string)(1:15)) then ! the 15 character subset is
> > > applied to the
> > > ! left justified
> > > string

> > Do you mean ?
> > else if (adjustl(string)(1:15) == 'some characters') then

> Yes.

How about

    else if (index(adjustl(string),'some characters') == 1) then

--
Qolin

Email: my qname at domain dot com
Domain: qomputing



Mon, 22 Aug 2011 07:25:31 GMT  
 String manipulation


Quote:

> How about

> ? ? else if (index(adjustl(string),'some characters') == 1) then

> --
> Qolin

Most excellent!   That will work perfectly.  And it's clear and
concise.


Tue, 23 Aug 2011 03:24:56 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

1. string manipulations

2. Question: String manipulation

3. String manipulation

4. String Manipulation with awk

5. String Manipulation

6. String Manipulation

7. How to do string manipulation in Eiffel?

8. String Manipulation in Forth

9. String Manipulation and Pulldown men

10. String Manipulation and Pulldown menu libraries for Forth

11. String Manipulation Routines

12. string manipulation

 

 
Powered by phpBB® Forum Software