github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/datatools/count_doc.yaml (about)

     1  - DocumentID: count
     2    Title: >+
     3      `count`
     4    CategoryID: commands
     5    Summary: >-
     6      Count items in a map, list or array
     7    Description: |-
     8  
     9    Usage: |-
    10      ```
    11      <stdin> -> count [ --duplications | --unique | --total ] -> <stdout>
    12      ```
    13    Examples: |-
    14      Count number of items in a map, list or array:
    15  
    16      ```
    17      » tout json (["a", "b", "c"]) -> count 
    18      3
    19      ```
    20    Detail: |-
    21      If no flags are set, `count` will default to using `--total`.
    22  
    23      ### Total: `--total` / `-t`
    24  
    25      This will read an array, list or map from STDIN and output the length for
    26      that array.
    27  
    28      ```
    29      » a [25-Dec-2020..05-Jan-2021] -> count 
    30      12
    31      ```
    32  
    33      > This also replaces the older `len` method.
    34  
    35      Please note that this returns the length of the _array_ rather than string.
    36      For example `out "foobar" -> count` would return `1` because an array in the
    37      `str` data type would be new line separated (eg `out "foo\nbar" -> count`
    38      would return `2`). If you need to count characters in a string and are
    39      running POSIX (eg Linux / BSD / OSX) then it is recommended to use `wc`
    40      instead. But be mindful that `wc` will also count new line characters.
    41  
    42      ```
    43      » out "foobar" -> count
    44      1
    45  
    46      » out "foo\nbar" -> count
    47      2
    48  
    49      » out "foobar" -> wc: -c
    50      7
    51  
    52      » out "foo\nbar" -> wc: -c
    53      8
    54  
    55      » printf "foobar" -> wc: -c
    56      6
    57      # (printf does not print a trailing new line)
    58      ```
    59  
    60      ### Duplications: `--duplications` / `-d`
    61  
    62      This returns a JSON map of items and the number of their occurrences in a list
    63      or array.
    64  
    65      For example in the quote below, only the word "the" is repeated so that entry
    66      will have a value of `2` while ever other entry has a value of `1` because they
    67      only appear once in the quote.
    68  
    69      ```
    70      » out "the quick brown fox jumped over the lazy dog" -> jsplit \s -> count --duplications
    71      {
    72          "brown": 1,
    73          "dog": 1,
    74          "fox": 1,
    75          "jumped": 1,
    76          "lazy": 1,
    77          "over": 1,
    78          "quick": 1,
    79          "the": 2
    80      }
    81      ```
    82  
    83      ### Unique: `--unique` / `-u`
    84  
    85      Returns the number of unique elements in a list or array.
    86  
    87      For example in the quote below, only the word "the" is repeated, thus the
    88      unique count should be one less than the total count:
    89  
    90      ```
    91      » out "the quick brown fox jumped over the lazy dog" -> jsplit \s -> count --unique
    92      8
    93      » out "the quick brown fox jumped over the lazy dog" -> jsplit \s -> count --total
    94      9
    95      ```
    96    Flags:
    97      "--duplications": >-
    98          Output a JSON map of items and the number of their occurrences in a list or array
    99      "-d": >- 
   100          Alias for `--duplications`
   101      
   102      "--unique": >-
   103          Print the number of unique elements in a list or array
   104      "-u": >-
   105          Alias for `--unique`
   106      
   107      "--sum": >-
   108          Read an array, list or map from STDIN and output the sum of all the values (ignore non-numeric values)
   109      "-s": >-
   110          Alias for `--sum`
   111      "--sum-strict": >-
   112          Read an array, list or map from STDIN and output the sum of all the values (error on non-numeric values)
   113  
   114      "--total": >-
   115          Read an array, list or map from STDIN and output the length for that array (default behaviour)
   116      "-t": >-
   117          Alias for `--total`
   118    Synonyms:
   119      - count
   120      - len
   121    Related:
   122      - prepend
   123      - append
   124      - map
   125      - a
   126      - ja
   127      - ta
   128      - jsplit
   129      - item-index
   130      - element
   131      - range
   132      - mtac
   133      - msort
   134      - tout
   135      - jsplit