Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
data4 = {{"050318", "111529"}}

Do[
 ID2 = data4[[i, 2]];
 s = Import["http://www.swift.ac.uk/xrt_curves/00" <> ID2, "Text"];
 s = StringSplit[s, "\n"];
 line = Position[s, "Flux Light Curve"],
 {i, 1, Length[data4]}]

The StringSplit works properly, but when I want to identify the position it gives line {} empty I expect instead a number like {{106}} for example

Even when I try with

Do[
 ID2 = data4[[i, 2]];
 s = Import["http://www.swift.ac.uk/xrt_curves/00" <> ID2, "Text"];
 s = StringSplit[s, "\n"];
 line = StringPosition[s, "Flux Light Curve"],
  {i, 1, Length[data4]}]

it gives me not a single line, but many empty ones.

share|improve this question

2 Answers 2

The string "Flux Light Curve" does not appear as an element of s. If you are looking for a substring you will need a different function.

If you only want to know the positions of strings which contain "Flux Light Curve" as a substring, you might use:

Position[ s, x_ /; StringMatchQ[x, __ ~~ "Flux Light Curve" ~~ __] ]

In this ~~ stands for StringExpression.

The output from this:

StringPosition[s, "Flux Light Curve"]

is a list with many empty lists as elements, because most elements in s do not contain the substring. You can further process the result as needed. For example:

x = StringPosition[s, "Flux Light Curve"];

Position[x, {__Integer}]

DeleteCases[x, {}]
share|improve this answer

Mr.Wizard's answer correctly identifies why your code wasn't working: you needed StringPosition rather than Position, and then to delete the empty cases.

I wonder if there isn't another way to attack your problem. You are importing an HTML file, so why not use that fact rather than treat it as plain text? You don't actually need all the HTML markup. So instead consider:

ss = Import["http://www.swift.ac.uk/xrt_curves/00111529", "HTML"];

This strips out the extraneous HTML stuff. So the first 100 characters are:

StringTake[ss, 100]
"Skip banner and navigation tools .  

   Dept. of Physics & Astronomy  
  XROA  
    US Site | "

Contrast this with

s = Import["http://www.swift.ac.uk/xrt_curves/0000111529", "Text"];
StringTake[s, 100]

enter image description here

(I had to insert this as a picture because inserting an HTML DOCTYPE statement really doesn't seem to work nicely with the StackExchange software, and rightly so!)

As you can see, you can obtain the positions of the desired substring within the whole, without having to split the string first.

StringPosition[ss, "Flux Light Curve"]

{{1797, 1812}, {2435, 2450}, {2465, 2480}}

Of course, you can always StringSplit in the same way and find the substring that contains the sub(sub)string:

sss = StringSplit[ss, "\n"];
StringPosition[sss, "Flux Light Curve"]
{{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {{52, 67}},
   {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
   {}, {}, {{1, 16}, {31, 46}}, {}, {}, {}, {}, {}, {}}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.