From: MikeD on


"Bill Betournay" <bill(a)REMOVEdatapacks.com> wrote in message
news:eGFx9F7xKHA.2012(a)TK2MSFTNGP04.phx.gbl...
> I've spent half the day today on try to figure out how to parse this
> string.
>
> Here it is
>
> I have a string like this: "1 1.1 1.2 1.3 1.4 1.5 1.6" and sometimes
> that string may have more that one carriage return like I have in my
> example between 1 & 1.1


Impossible to tell from your example that there's a carriage return between
"1" and "1.1". Looks like a double space to me with a single space between
all the other numbers. <g>

>
> What I need is to separate out each number like
>
> 1
> 1.1
> 1.2
> 1.3
> 1.4
> 1.5
> 1.6
>
> I've been googling and sifting through help files looking for a way to
> parse it out but I can't seem to find any. This has to be an easy task and
> I sure would appreciate any help at all.
>

Break it down into steps. If you had to do this manually, what steps would
you take? Figure out the steps necessary and then just write the code to do
each of those steps. Get it to work first, and then you'll probably find you
can combine some steps to shorten/optimize the code. But at first, don't
worry about how much code you have to write or how many steps are involved.
If you have to break it down into 20 steps, then do so. To give you a hint,
it sounds like the Split function is almost definitely something you'll want
to use. If you're not familiar with Split, it creates an array from a string
of delimited values. In this case, the delimiter is a CR. What complicates
matters somewhat (and why you need to break it down into steps) is that the
delimiter apparently could be doubled (or possibly tripled or even
quadrupled) between values.

Parsing is not that difficult if you break it down into steps. If you try
to parse the whole thing using just 1 or 2 statements, it can be a
nightmare, and for some reason, that's the impression I'm getting that
you're trying to do. Resist that temptation. As I said, you can optimize the
code AFTER you've got it doing what you need, if even necessary.

Spend some more time on it and if you still can't get anywhere, post back.
Either myself or someone else can surely write up something to at least get
you going (there ARE unknowns, so it's not likely we'd be able to write
anything that exactly meets your needs without additional information. Just
for an example, could there be any number of CRs between numbers? If so,
you'd probably just need a loop to eliminate any "extras" before using
Split, but you could also Split first and eliminate afterwards (but that'd
probably involve more work than eliminating them before using Split).

--
Mike


From: MikeD on


"Mike Williams" <Mike(a)WhiskyAndCoke.com> wrote in message
news:uss5DV7xKHA.5036(a)TK2MSFTNGP02.phx.gbl...
> "Bill Betournay" <bill(a)REMOVEdatapacks.com> wrote in message
> news:eGFx9F7xKHA.2012(a)TK2MSFTNGP04.phx.gbl...
>
>> I've spent half the day today on try to figure out how
>> to parse this string. Here it is. I have a string like this:
>> "1 1.1 1.2 1.3 1.4 1.5 1.6" and sometimes that string
>> may have more that one carriage return like I have in
>> my example between 1 & 1.1
>
> You mean more than one space?
>
>> What I need is to separate out each number like 1
>> 1.1
>> 1.2 . . etc
>
> There are loads of different ways of doing it. Here's one way that will
> parse it no matter how many spaces there are between the individual
> values:
>
> Private Sub Command1_Click()
> Dim s1 As String, s2() As String, n As Long
> s1 = "1 1.1 1.2 1.3 1.4 1.5 1.6"
> Do
> n = Len(s1)
> s1 = Replace(s1, " ", " ")
> Loop Until Len(s1) = n
> s2 = Split(s1, " ")
> ' display the result:
> For n = LBound(s2) To UBound(s2)
> Print n, s2(n)
> Next n
> End Sub
>


Whatever happened to trying to get someone to think on their own? <g>

I could have given code in my answer. I chose not to in favor of giving
hints and advice for him to work it out himself. He's not going to learn
anything from hand-outs (except to come to the newsgroups to be given code).
If people want to be programmers, the first thing to learn is how to solve
the challenges you'll undoubtedly face. You don't learn the methodology of
doing that by being given the code (well, a few people MIGHT actually study
the code to understand it; my experience is that most don't).

(not trying to start anything, Mike. Just making a light-hearted comment, so
don't get all defensive. <g>)

--
Mike




From: GS on
Bill Betournay brought next idea :
> I've spent half the day today on try to figure out how to parse this string.
>
> Here it is
>
> I have a string like this: "1 1.1 1.2 1.3 1.4 1.5 1.6" and sometimes that
> string may have more that one carriage return like I have in my example
> between 1 & 1.1
>
> What I need is to separate out each number like
>
> 1
> 1.1
> 1.2
> 1.3
> 1.4
> 1.5
> 1.6
>
> I've been googling and sifting through help files looking for a way to parse
> it out but I can't seem to find any. This has to be an easy task and I sure
> would appreciate any help at all.
>
> Thank you
>
> Bill

If you expect that CRLFs and multiple spaces are the only variables in
the structure of your string to parse then I suggest doing something
like the following:

Dim vCRLFs As Variant, vSPs As Variant, vSz As Variant
Dim sTemp As String

'1st step:
'Split the original string into smaller strings according to CRLFs
vCRLFs = Split(StringToParse, vbCrLf)

'2nd step:
'Now filter out the empty elements into a dummy array
vSz = Filter(vCRLFs, "", False)

3rd step:
'At this point you need to dump the array back into a string without
CRLFs, delimited only by a space
sTemp = Join(vSz, " ")

4th step:
'Split the dummy string into smaller strings once again according to
spaces
vSPs = Split(sTemp, " ")

5th step:
'Now filter out the empty elements once more
vSz = Empty 'clear the variable of contents
vSz = Filter(vSPs, "", False)

Now you should have an array (vSz) containing only valid values for
processing however you like. Iterate this to extract each value for
further processing.

This could probably be optimized for better code efficiency but that
can be done after you get it working consistently, and with reliable
results.

HTH
--

Garry


From: Mike Williams on
"MikeD" <nobody(a)nowhere.edu> wrote in message
news:eHdB1T8xKHA.4752(a)TK2MSFTNGP04.phx.gbl...


> Whatever happened to trying to get someone to think on their
> own? <g> I could have given code in my answer. I chose not
> to in favor of giving hints and advice for him to work it out
> himself. He's not going to learn anything from hand-outs (except
> to come to the newsgroups to be given code) . . .
> (not trying to start anything, Mike. Just making a light-hearted
> comment, so don't get all defensive. <g>)

Actually you're quite right, Mike. I really should have done what you
suggested, especially after taking into account the faitly obvious learning
stage of the OP. I hang my head in shame ;-)

Mike


From: Bill Betournay on
Good Morning guys. Thank you both for your offerings. MikeD, I was stepping
it through building my solution and then discovered the problem of parsing
out the ?? Object ( think it's a carriage return) from the list. I hope I'm
not missing your point.

And Mike Williams, that example you provided was great in the sense that you
seem to understand. Problem is the ability to capture the carriage return.
That's my issue. You example takes my list and rips it through a bunch of
loops but returns the same list. In fact the string example below was the
result of your example. It's not capturing the stuff between the numbers
either. I don't understand why I can't seem to get the whatever it is
between each set of numbers

Thank you both and yes, this may have changed a little since my OP because
I've used your examples and learned a little more. :-)

here's what I've tried

Replace(myInputStr, vbCr, ",")
Replace(myInputStr, vbCrLf, ",")
Replace(myInputStr, vbCr, ",")
Replace(myInputStr, Chr$(13), ",")
Replace(myInputStr, Chr(13), ",")

here's an example of a string I have.
"8


8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8
8.9
8.10
8.11
8.12
8.13
8.14
8.15
8.16
8.17
8.18
8.19
8.20
8.21
8.22
8.23
8.24
8.25
8.26"

and ultimately here what I would like
"8,8.1,8.2,8.3,8.4,8.5,8.6,8.7,8.8,8.9,8.10,8.11,8.12,8.13,8.14,8.15,8.16,8.17,8.18,8.19,8.20,8.21,8.22,8.23,8.24,8.25,8.26"

Does that clear

"Mike Williams" <Mike(a)WhiskyAndCoke.com> wrote in message
news:eHXD19ByKHA.984(a)TK2MSFTNGP05.phx.gbl...
> "MikeD" <nobody(a)nowhere.edu> wrote in message
> news:eHdB1T8xKHA.4752(a)TK2MSFTNGP04.phx.gbl...
>
>
>> Whatever happened to trying to get someone to think on their
>> own? <g> I could have given code in my answer. I chose not
>> to in favor of giving hints and advice for him to work it out
>> himself. He's not going to learn anything from hand-outs (except
>> to come to the newsgroups to be given code) . . .
>> (not trying to start anything, Mike. Just making a light-hearted
>> comment, so don't get all defensive. <g>)
>
> Actually you're quite right, Mike. I really should have done what you
> suggested, especially after taking into account the faitly obvious
> learning stage of the OP. I hang my head in shame ;-)
>
> Mike
>
>


First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9
Prev: Combobox Showing Right Justified
Next: Can't make exe