github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/commands/count.md (about)

     1  # `count`
     2  
     3  > Count items in a map, list or array
     4  
     5  ## Description
     6  
     7  
     8  
     9  ## Usage
    10  
    11  ```
    12  <stdin> -> count [ --duplications | --unique | --total ] -> <stdout>
    13  ```
    14  
    15  ## Examples
    16  
    17  Count number of items in a map, list or array:
    18  
    19  ```
    20  » tout json (["a", "b", "c"]) -> count 
    21  3
    22  ```
    23  
    24  ## Flags
    25  
    26  * `--duplications`
    27      Output a JSON map of items and the number of their occurrences in a list or array
    28  * `--sum`
    29      Read an array, list or map from STDIN and output the sum of all the values (ignore non-numeric values)
    30  * `--sum-strict`
    31      Read an array, list or map from STDIN and output the sum of all the values (error on non-numeric values)
    32  * `--total`
    33      Read an array, list or map from STDIN and output the length for that array (default behaviour)
    34  * `--unique`
    35      Print the number of unique elements in a list or array
    36  * `-d`
    37      Alias for `--duplications`
    38  * `-s`
    39      Alias for `--sum`
    40  * `-t`
    41      Alias for `--total`
    42  * `-u`
    43      Alias for `--unique`
    44  
    45  ## Detail
    46  
    47  If no flags are set, `count` will default to using `--total`.
    48  
    49  ### Total: `--total` / `-t`
    50  
    51  This will read an array, list or map from STDIN and output the length for
    52  that array.
    53  
    54  ```
    55  » a [25-Dec-2020..05-Jan-2021] -> count 
    56  12
    57  ```
    58  
    59  > This also replaces the older `len` method.
    60  
    61  Please note that this returns the length of the _array_ rather than string.
    62  For example `out "foobar" -> count` would return `1` because an array in the
    63  `str` data type would be new line separated (eg `out "foo\nbar" -> count`
    64  would return `2`). If you need to count characters in a string and are
    65  running POSIX (eg Linux / BSD / OSX) then it is recommended to use `wc`
    66  instead. But be mindful that `wc` will also count new line characters.
    67  
    68  ```
    69  » out "foobar" -> count
    70  1
    71  
    72  » out "foo\nbar" -> count
    73  2
    74  
    75  » out "foobar" -> wc: -c
    76  7
    77  
    78  » out "foo\nbar" -> wc: -c
    79  8
    80  
    81  » printf "foobar" -> wc: -c
    82  6
    83  # (printf does not print a trailing new line)
    84  ```
    85  
    86  ### Duplications: `--duplications` / `-d`
    87  
    88  This returns a JSON map of items and the number of their occurrences in a list
    89  or array.
    90  
    91  For example in the quote below, only the word "the" is repeated so that entry
    92  will have a value of `2` while ever other entry has a value of `1` because they
    93  only appear once in the quote.
    94  
    95  ```
    96  » out "the quick brown fox jumped over the lazy dog" -> jsplit \s -> count --duplications
    97  {
    98      "brown": 1,
    99      "dog": 1,
   100      "fox": 1,
   101      "jumped": 1,
   102      "lazy": 1,
   103      "over": 1,
   104      "quick": 1,
   105      "the": 2
   106  }
   107  ```
   108  
   109  ### Unique: `--unique` / `-u`
   110  
   111  Returns the number of unique elements in a list or array.
   112  
   113  For example in the quote below, only the word "the" is repeated, thus the
   114  unique count should be one less than the total count:
   115  
   116  ```
   117  » out "the quick brown fox jumped over the lazy dog" -> jsplit \s -> count --unique
   118  8
   119  » out "the quick brown fox jumped over the lazy dog" -> jsplit \s -> count --total
   120  9
   121  ```
   122  
   123  ## Synonyms
   124  
   125  * `count`
   126  * `len`
   127  
   128  
   129  ## See Also
   130  
   131  * [`[ ..Range ]`](../parser/range.md):
   132    Outputs a ranged subset of data from STDIN
   133  * [`[ Index ]`](../parser/item-index.md):
   134    Outputs an element from an array, map or table
   135  * [`[[ Element ]]`](../parser/element.md):
   136    Outputs an element from a nested structure
   137  * [`a` (mkarray)](../commands/a.md):
   138    A sophisticated yet simple way to build an array or list
   139  * [`append`](../commands/append.md):
   140    Add data to the end of an array
   141  * [`ja` (mkarray)](../commands/ja.md):
   142    A sophisticated yet simply way to build a JSON array
   143  * [`jsplit` ](../commands/jsplit.md):
   144    Splits STDIN into a JSON array based on a regex parameter
   145  * [`jsplit` ](../commands/jsplit.md):
   146    Splits STDIN into a JSON array based on a regex parameter
   147  * [`map`](../commands/map.md):
   148    Creates a map from two data sources
   149  * [`msort`](../commands/msort.md):
   150    Sorts an array - data type agnostic
   151  * [`mtac`](../commands/mtac.md):
   152    Reverse the order of an array
   153  * [`prepend`](../commands/prepend.md):
   154    Add data to the start of an array
   155  * [`ta` (mkarray)](../commands/ta.md):
   156    A sophisticated yet simple way to build an array of a user defined data-type
   157  * [`tout`](../commands/tout.md):
   158    Print a string to the STDOUT and set it's data-type
   159  
   160  <hr/>
   161  
   162  This document was generated from [builtins/core/datatools/count_doc.yaml](https://github.com/lmorg/murex/blob/master/builtins/core/datatools/count_doc.yaml).