MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Strings to Integers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67638] Re: [mg67614] Re: [mg67566] Strings to Integers
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Mon, 3 Jul 2006 06:37:44 -0400 (EDT)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

Bruce,

It works as desired with RuleDelayed (note that a space is required after the blank)

StringCases["12:00",
  x__~~":"~~y__ :>
    {ToExpression@x,ToExpression@y}]

{{12, 0}}

Map[Head,%[[1]]]

{Integer,Integer}

This can be written more compactly as

StringCases["12:00",
  x__~~":"~~y__ :> ToExpression@{x,y}]

{{12, 0}}


Bob Hanlon

---- Bruce Colletti <vze269bv at verizon.net> wrote: 
> Bob
> 
> I like your and Paul's solutions better than mine.  The following baffles me, though...
> 
> Your solution delivers the desired list of integers:
> 
> ToExpression@StringSplit["12:00",":"]
> Map[Head,%]
> 
> {12,0}
> 
> {Integer,Integer}
> 
> But why doesn't my solution work:
> 
> StringCases["12:00",x__~~":"~~y__->{ToExpression@x,ToExpression@y}]
> 
> Map[Head,%[[1]]]
> 
> {{12,00}}
> 
> {String,String}
> 
> I'm using v5.2.0.0.  Thanks.
> 
> Bruce
> 
> =====================
> From: Bob Hanlon <hanlonr at cox.net>
To: mathgroup at smc.vnet.net
> Subject: [mg67638] [mg67614] Re: [mg67566] Strings to Integers
> 
> data = {"12:33", "120:03", "343:59", "1:00"};
> 
> ToExpression[StringSplit[#, ":"]& /@ data]
> 
> {{12, 33}, {120, 3}, {343, 59}, {1, 0}}
> 
> 
> Bob Hanlon
> 
> ---- Virgil Stokes <vs at it.uu.se> wrote: 
> > The following is an example of a list that contains times in min:sec,
> > 
> > {12.33, 120:03, 343:59, 1:00}
> > 
> > where each element is of String type.
> > 
> > Is there a "smart" way to convert lists such as this into Integer type 
> > for both minutes and seconds?
> > 
> > --Thanks,
> > V. Stokes
> > 
> 


  • Prev by Date: Re: calling maximize recursively
  • Next by Date: Re: Re: Re: Limit of an expression?
  • Previous by thread: Re: Re: Strings to Integers
  • Next by thread: Re: Strings to Integers