2

I am dealing with a string of numbers in scientific format. for example

24  6.924E+06  8.316E-01  1.052E-01  1.622E+01  1.311E+01  0.000E+00  6.059E-06 (snip.. extends for a bit)

Now I want to write a regex for perl which allows me to capture the ith value in the list. So my current set up is the folloiwng

$_ =~ ^\s+\d+\s+(\d+[.]\d+E[+]\d+);
my $temp = $1;

Which will get me the first number. I want to be able to capture the 7th or the 50th if I wanted without having to write a really long regex expression.

is there a concise way of doing this?

Thanks in advance.

Miller
  • 34,344
  • 4
  • 33
  • 55
TheCodeNovice
  • 466
  • 8
  • 30

3 Answers3

6

use split

my @cols = split ' ', $_;

my $seventh = $cols[6];
my $fiftieth = $cols[49];
Miller
  • 34,344
  • 4
  • 33
  • 55
2

split is the best option for this case.

my @val = split ' ', $_;
my $val7 = $val[6];
parthi
  • 156
  • 1
  • 8
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – web-tiki Jul 19 '14 at 08:15
  • @web-tiki: this answer is exactly the same as [Miller's](http://stackoverflow.com/a/24833984/20938), which has six upvotes as I write this. – Alan Moore Jul 19 '14 at 08:52
  • @AlanMoore if you check the edit history of this answer, you will see it was edited after my comment. – web-tiki Jul 19 '14 at 08:55
  • 1
    I see! So it morphed from a non-answer to a duplicate answer. Objection retracted. – Alan Moore Jul 19 '14 at 09:00
0

An example for the third number:

^\s+\d+(?:\s+(\d+\.\d+E[-+]?\d+)){3}

When you repeat a capture group, the content is overwritten with the last match.

NB: in this case, it is more clean to use [0-9] instead of \d since you only need arabic digits.

Casimir et Hippolyte
  • 83,228
  • 5
  • 85
  • 113