4
$\begingroup$

I have A list that contains sublists like that:

    [{"Full ", 0.0003` "_r arcmin", "04 32 01.842" "_RAJ2000 \"h:m:s\"", 
  "+53 54 39.04" "_DEJ2000 \"d:m:s\"", 21148 "HIP ", "n_ " "P", 
  15 "Sn ", 3 "So ", 68.00768144` "RArad deg", 
  53.91084624` "DErad deg", -4.6` "Plx mas", 
  1.85` "e_ mas", -1.29` "pmRA mas/yr", -0.17` "pmDE mas/yr", 
  5.8249` "Hpmag mag", 0.119` "B-V mag", "HIP1" "HIP1 "} ];

And I need the Plx values as its HIP , as:

{-4.6` ,21148};

A tried Cases, and Select but it did not work:

Select or Cases[REAL_ ~~ "Plx mas",REAL_~~"HIP "][List]:

The List:

{{"Full ", 0.0003 "_r arcmin", "04 32 01.842" "_RAJ2000 \"h:m:s\"", 
  "+53 54 39.04" "_DEJ2000 \"d:m:s\"", 21148 "HIP ", "n_ " "P", 
  15 "Sn ", 3 "So ", 68.0077 "RArad deg", 
  53.9108 "DErad deg", -4.6 "Plx mas", 
  1.85 "e_ mas", -1.29 "pmRA mas/yr", -0.17 "pmDE mas/yr", 
  5.8249 "Hpmag mag", 0.119 "B-V mag", "HIP1" "HIP1 "}, {"Full ", 
  0.0008 "_r arcmin", "22 39 15.679" "_RAJ2000 \"h:m:s\"", 
  "+39 03 00.97" "_DEJ2000 \"d:m:s\"", 111841 "HIP ", "" "n_ ", 
  5 "Sn ", 0, 339.815 "RArad deg", 39.0503 "DErad deg", 
  1.89 "Plx mas", 
  0.22 "e_ mas", -0.32 "pmRA mas/yr", -5.46 "pmDE mas/yr", 
  4.8135 "Hpmag mag", -0.207 "B-V mag", "HIP1" "HIP1 "}, {"Full ", 
  0.0006 "_r arcmin", "22 02 04.573" "_RAJ2000 \"h:m:s\"", 
  "+58 00 01.31" "_DEJ2000 \"d:m:s\"", 108772 "HIP ", "n_ " "P", 
  5 "Sn ", 0, 330.519 "RArad deg", 58.0004 "DErad deg", 
  1.07 "Plx mas", 
  0.18 "e_ mas", -2.72 "pmRA mas/yr", -2.74 "pmDE mas/yr", 
  5.546 "Hpmag mag", 0.017 "B-V mag", "HIP1" "HIP1 "}, {"Full ", 
  0.0483 "_r arcmin", "06 40 58.660" "_RAJ2000 \"h:m:s\"", 
  "+09 53 44.72" "_DEJ2000 \"d:m:s\"", 31978 "HIP ", "n_ " "W", 
  15 "Sn ", 3 "So ", 100.244 "RArad deg", 9.89576 "DErad deg", 
  3.55 "Plx mas", 
  0.5 "e_ mas", -2.61 "pmRA mas/yr", -1.61 "pmDE mas/yr", 
  4.5554 "Hpmag mag", -0.233 "B-V mag", "HIP1" "HIP1 "}, {"Full ", 
  0.0003 "_r arcmin", "18 15 12.906" "_RAJ2000 \"h:m:s\"", 
  "-20 43 41.77" "_DEJ2000 \"d:m:s\"", 89439 "HIP ", "" "n_ ", 
  7 "Sn ", 0, 273.804 "RArad deg", -20.7283 "DErad deg", 
  0.1 "Plx mas", 0.45 "e_ mas", 
  1.6 "pmRA mas/yr", -1.51 "pmDE mas/yr", 5.3438 "Hpmag mag", 
  0.007 "B-V mag", "HIP1" "HIP1 "}}
$\endgroup$

3 Answers 3

5
$\begingroup$

Using same data name as kguler

Cases[data, Times[x_, "Plx mas" | "HIP "] :> x, -1]
(*{21148, -4.6, 111841, 1.89, 108772, 1.07, 31978, 3.55, 89439, 0.1}*)

First /@ Select[data // Flatten, 
  Length@# >= 2 && (#[[2]] === "Plx mas" || #[[2]] === "HIP ") &]
 (*{21148, -4.6, 111841, 1.89, 108772, 1.07, 31978, 3.55, 89439, 0.1}*)
$\endgroup$
6
$\begingroup$

With data as in kguler's answer:

data /. {___, b_ "HIP ", ___, a_ "Plx mas", ___} :> {a, b}
(* Out: {{-4.6, 21148}, {1.89, 111841}, {1.07, 108772}, {3.55, 31978}, {0.1, 89439}} *)
$\endgroup$
4
$\begingroup$
Cases[data, (r_Real|r_Integer) s_String?(StringMatchQ[#, "*HIP*"|"*Plx*"] &) :> r, {0, Infinity}]
(* {21148, -4.6, 111841, 1.89, 108772, 1.07, 31978, 3.55, 89439, 0.1} *)

where

data = {{"Full ", 0.0003 "_r arcmin", 
   "04 32 01.842" "_RAJ2000 \"h:m:s\"", 
   "+53 54 39.04" "_DEJ2000 \"d:m:s\"", 21148 "HIP ", "n_ " "P", 
   15 "Sn ", 3 "So ", 68.0077 "RArad deg", 
   53.9108 "DErad deg", -4.6 "Plx mas", 
   1.85 "e_ mas", -1.29 "pmRA mas/yr", -0.17 "pmDE mas/yr", 
   5.8249 "Hpmag mag", 0.119 "B-V mag", "HIP1" "HIP1 "}, {"Full ", 
   0.0008 "_r arcmin", "22 39 15.679" "_RAJ2000 \"h:m:s\"", 
   "+39 03 00.97" "_DEJ2000 \"d:m:s\"", 111841 "HIP ", "" "n_ ", 
   5 "Sn ", 0, 339.815 "RArad deg", 39.0503 "DErad deg", 
   1.89 "Plx mas", 
   0.22 "e_ mas", -0.32 "pmRA mas/yr", -5.46 "pmDE mas/yr", 
   4.8135 "Hpmag mag", -0.207 "B-V mag", "HIP1" "HIP1 "}, {"Full ", 
   0.0006 "_r arcmin", "22 02 04.573" "_RAJ2000 \"h:m:s\"", 
   "+58 00 01.31" "_DEJ2000 \"d:m:s\"", 108772 "HIP ", "n_ " "P", 
   5 "Sn ", 0, 330.519 "RArad deg", 58.0004 "DErad deg", 
   1.07 "Plx mas", 
   0.18 "e_ mas", -2.72 "pmRA mas/yr", -2.74 "pmDE mas/yr", 
   5.546 "Hpmag mag", 0.017 "B-V mag", "HIP1" "HIP1 "}, {"Full ", 
   0.0483 "_r arcmin", "06 40 58.660" "_RAJ2000 \"h:m:s\"", 
   "+09 53 44.72" "_DEJ2000 \"d:m:s\"", 31978 "HIP ", "n_ " "W", 
   15 "Sn ", 3 "So ", 100.244 "RArad deg", 9.89576 "DErad deg", 
   3.55 "Plx mas", 
   0.5 "e_ mas", -2.61 "pmRA mas/yr", -1.61 "pmDE mas/yr", 
   4.5554 "Hpmag mag", -0.233 "B-V mag", "HIP1" "HIP1 "}, {"Full ", 
   0.0003 "_r arcmin", "18 15 12.906" "_RAJ2000 \"h:m:s\"", 
   "-20 43 41.77" "_DEJ2000 \"d:m:s\"", 89439 "HIP ", "" "n_ ", 
   7 "Sn ", 0, 273.804 "RArad deg", -20.7283 "DErad deg", 
   0.1 "Plx mas", 0.45 "e_ mas", 
   1.6 "pmRA mas/yr", -1.51 "pmDE mas/yr", 5.3438 "Hpmag mag", 
   0.007 "B-V mag", "HIP1" "HIP1 "}};
$\endgroup$
3
  • $\begingroup$ I didnt know about this command : (r_Real|r_Integer) s_String?; Could you explain more about its applications? $\endgroup$
    – locometro
    Commented Oct 7, 2014 at 22:15
  • 2
    $\begingroup$ the list has elements that are reals or integers multiplied by strings therefore the pattern used for testing is (r_Real|r_Integer) s_String. In addition to that the OP wanted a constraint on the string so the string has to be tested to make sure it matches either Plx or HIP. If you get a pattern match then you want the value of the real or integer, r, returned. $\endgroup$ Commented Oct 7, 2014 at 22:18
  • $\begingroup$ Clean, and elegant way to get mixed elements, I did not realize that Number was multiplying string. Thanks. $\endgroup$
    – locometro
    Commented Oct 7, 2014 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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