Re: String to Number ?
From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 05/10/04
- Next message: Bob Butler: "Re: container name"
- Previous message: lance: "container name"
- In reply to: Kevala: "Re: String to Number ?"
- Next in thread: Kevala: "Re: String to Number ?"
- Reply: Kevala: "Re: String to Number ?"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 10 May 2004 11:04:23 -0400
I guess it's up to me to provide the one-liner solution? <g>
I'm guessing the space-dash-space characters, taken together, is the
actual delimiter between the comment and the numbers (and that each
record ends with the shown <br>), so... if this is a line of text you
got from your file
Record = "Aflatoxin_liver (a liver-damaging toxin produced by certain
food molds) - 344, 510.1, 943, 474, 476.6, 568<br>"
then this will store the number parts in a variant array.
Numbers = Split(Split(Replace$(Record, "<br>", ""), " - ")(1), ",")
which you can address as you would any other array. For example, here is
a listing of 100 times each number in the record string...
For X = 0 To UBound(Nums)
Debug.Print Numbers(X)
Next
By the way, you would Dim Numbers As Variant when declaring it. As an
array of variant values, the individual values in the Numbers array are
either Strings or numbers as necessary. Note that variants are
**slower** to work with than "normal" data types, so this solution is
not necessarily considered efficient. If you have a very large number of
records to process, then this solution will be somewhat slower than the
others posted. Also Split and Replace are considered slow functions too;
although on such a small string of characters, the inefficiency here
should be minimal. The solution I posted is more of an "interesting
construction" than a general, use-it-everywhere proposal. (I've posted
it more for those regulars out there who expect me to come up with
one-liners.<g>)
Rick - MVP
"Kevala" <kevala@pacific.net.au> wrote in message
news:VkHnc.18389$KS1.321504@nasal.pacific.net.au...
> Thank you,
>
> Sorry for not quoting an entire string as a sample. Below is one of
the
> strings I am processing.
>
> Aflatoxin_liver (a liver-damaging toxin produced by certain food
molds) -
> 344, 510.1, 943, 474, 476.6, 568<br>
>
> I have been marching along the string in a loop using the Mid
function. The
> first trigger for the end of the description is the space character.
The
> trigger for end of comments is the ")" character. Then follows the
numbers.
> I just count over the three characters after the ")" character before
the
> first digit of the number begins. The trigger for the last number is
the "<"
> character in <br>
>
>
> "J French" <erewhon@nowhere.com> wrote in message
> news:409f2d26.10824226@news.btclick.com...
> > On Mon, 10 May 2004 14:40:09 +1000, "Kevala" <kevala@pacific.net.au>
> > wrote:
> >
> > >Hello,
> > >
> > >I am reading a text file in VB6 using line input.
> > >
> > >The first part of the line input string is comments that stay as
strings.
> > >
> > >The last part of the line input string is formatted as such ...
> > >
> > >"788.6, 13.29, 340.2" ... etc to the end of the line.
> > >
> > >I am currently reading each character and building strings for
> distributing
> > >to a database.
> > >
> > >My question is, is there an easy way of converting a string such as
> "788.6"
> > >to the same value and placing it into a single precision variable?
> > >
> > >Or as I have thought, just writing a temp file, dumping the string
to a
> file
> > >and then re-opening it and reading the data back in as single
precision
> > >variables.
> > >
> >
> > If you /know/ the data uses '.' for the Decimal seperator then use
Val
> > eg:
> > MySingle = Val( TheString )
> >
> > If you need it to be locale aware then:
> > MySingle = CLng( TheString )
> >
> >
>
>
- Next message: Bob Butler: "Re: container name"
- Previous message: lance: "container name"
- In reply to: Kevala: "Re: String to Number ?"
- Next in thread: Kevala: "Re: String to Number ?"
- Reply: Kevala: "Re: String to Number ?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|