3

Using J I am trying to do something similar to the following example shown on page 128 of Mastering Dyalog APL by Bernard Legrand (2009). I have not been able to find a direct conversion of this code into J, which is what I want.

Here's the example:

BHCodes ← 83 12 12 83 43 66 50 81 12 83 14 66 etc...
BHAmounts ← 609 727 458 469 463 219 431 602 519 317 663 631...

13.3.2 - First Question

We would like to focus on some selected countries (14, 43, 50, 37, and 66) and calculate the total amount of their sales. Let’s first identify which items of BHCodes are relevant:

      Selected ← 14 43 50 37 66
      BHCodes  ∊ Selected
0 0 0 0 1 1 1 0 0 0 1 1 0 1 0     ⇦ Identifies sales in the selected countries only.

Then we can apply this filter to the amounts, and add them up:

     (BHCodes ∊ Selected) / BHAmounts
463 219 431 663 631 421
     +/ (BHCodes ∊ Selected) / BHAmounts
2828
Adám
  • 6,573
  • 20
  • 37
Jamie
  • 150
  • 6

1 Answers1

7
+/ (BHCodes e. Selected) # BHAmounts

For your purposes here, APL's is J's e. (Member (In)) and APL's / is J's # (Copy).

Notes:

  1. APL's and J's e. are not completely equivalent as APL's looks for every element in its left argument among the elements of its right argument, while J's e. looks for every major cell. of its left argument in the major cells of its right argument.

  2. APL's / and J's # are not completely equivalent as APL's / operates along the trailing axis while J's # operates along the leading axis. APL does have though, which operates along the leading axis. There are more nuances, but they are not relevant here.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Adám
  • 6,573
  • 20
  • 37