Here is one way to do it. It assumes the presence of a semicolon to
identify the two parts.
tstrings = {"06:42", "12:30", "1:22", "13:45"};
In[57]:=
convert[t_String] := (t //
Characters) /. {{a__, ":",
b__} :> (ToExpression[StringJoin[#]] & /@ {{a}, {b}})}
In[58]:=
convert /@ tstrings
Out[58]=
{{6, 42}, {12, 30}, {1, 22}, {13, 45}}
Ken Levasseur
Math Sciences
UMass Lowell
stef...@server2.fo.FH-Koeln.DE wrote:
> Hi
>
> I read a time from a file (this format 06:42) as a string.
>
> I would like to convert this string into to numbers (6 and 42).
>
> any suggestions?
>
> Thanks
>
> Steffen
>
>
> Dipl.-Ing. Steffen Zozgornik
>
> Institut fur Licht- und Bautechnik
> an der Fachhochschule Koeln
> Gremberger Strasse 151 a
> D - 51105 Koeln
>
> Tel.: +49 - 221 - 83 10 95
> Fax.: +49 - 221 - 83 55 13
stef...@server2.fo.FH-Koeln.DE wrote:
> Hi
>
> I read a time from a file (this format 06:42) as a string.
>
> I would like to convert this string into to numbers (6 and 42).
>
> any suggestions?
>
> Thanks
>
> Steffen
>
>
> Dipl.-Ing. Steffen Zozgornik
>
> Institut fur Licht- und Bautechnik
> an der Fachhochschule Koeln
> Gremberger Strasse 151 a
> D - 51105 Koeln
>
> Tel.: +49 - 221 - 83 10 95
> Fax.: +49 - 221 - 83 55 13
Hello Steffen,
Here are some solutions:
In[11]:=
str = {"06:42", "07:56", "11:23"};
pos = {2, {4, 5}};
li = Flatten[Table[{i, #} & /@ {pos[[1]], pos[[2]]}, {i, 1,
Length[str]}], 1];
In[34]:=
newvars1 = Partition[ToExpression[StringTake[str[[#1]], #2]] & @@@ li,
2];
newvars2 =
Table[#1[#2[#3, pos[[j]]]], {j, 1, 2}] &[ToExpression, StringTake,
str[[#]]] & /@ {1, 2, 3};
newvars3 =
Partition[
Map[StringTake, Sequence @@ {str[[#1]], #2} &] @@@ li //
ToExpression,
2];
In[37]:=
Scan[Print, {li, newvars1, newvars2, newvars3}]
{{1, 2}, {1, {4, 5}}, {2, 2}, {2, {4, 5}}, {3, 2}, {3, {4, 5}}}
{{6, 42}, {7, 56}, {11, 23}}
{{6, 42}, {7, 56}, {11, 23}}
{{6, 42}, {7, 56}, {11, 23}}
Helge Andersson
Environmental Inorganic Chemistry
Chalmers
SE-412 96 Göteborg
Tel. 031-772 8072
Fax 031-772 2853
If the string is always in the form "mm:ss" you could do this:
str = "06:42"
{minutes, seconds} = ToExpression /@ {StringTake[str, 2],
StringTake[str, -2]}
{6, 42}
If the minutes could go beyond 99 and the seconds could have a decimal
point, and there is always one and only one colon, then you could use this.
toMinutesSeconds[str_] :=
With[{poscolon = StringPosition[str, ":"][[1,1]]},
ToExpression /@ {StringTake[str, poscolon - 1],
StringDrop[str, poscolon]}]
str2 = "124:23.456"
{minutes, seconds} = toMinutesSeconds[str2]
{124, 23.456}
David Park
dj...@earthlink.net
http://home.earthlink.net/~djmp/
In[1]:=
makeNumbers[x_String] :=
ToExpression[{If[StringTake[x, 1] == "0", StringTake[x, {2}],
StringTake[x, 2]],
If[StringTake[x, 3] == "0", StringTake[x, {-2}], StringTake[x, -2]]}]
In[2]:=
makeNumbers["06:42"]
Out[2]=
{6, 42}
In[3]:=
makeNumbers["12:28"]
Out[3]=
{12, 28}
In[4]:=
makeNumbers["12:03"]
Out[4]=
{12, 3}
Tomas Garza
Mexico City
thank You for Your function, but the zeros are "deleted"
automatically after conversion into a number.
Steffen
> Date: Thu, 08 Feb 2001 12:31:51 -0600
> From: Tomas Garza <tgar...@prodigy.net.mx>
> Subject: Re: [mg27156] String to Number > To: stef...@server2.fo.FH-Koeln.DE
> Cc: math...@smc.vnet.net
Dipl.-Ing. Steffen Zozgornik
Institut für Licht- und Bautechnik
an der Fachhochschule Köln
Gremberger Straße 151 a
D - 51105 Köln
Yes, of course they are. That is because when you perform this example --
> > makeNumbers["06:42"]
You get this:
> > Out[2]=
> > {6, 42}
There is no number "06." Numbers don't have leading zeros.
Maybe we are not understanding your question as you intend it.
In your original post, you asked this:
> > > I read a time from a file (this format 06:42) as a string.
> > >
> > > I would like to convert this string into to numbers (6 and 42).
The reply by Mr. Garza does this.
--
Paul Lutus
www.arachnoid.com