Skip to main content
deleted 1 character in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

bash associative arrays (for which you need a recent version) don't support arbitrary keys. It also has no operator for sorting lists (which you'd need for that last to make sense), and can't do floating point arithmetics.

Here, I'd use a less limited shell like zsh or a proper programming language like perl.

#! /usr/bin/perl
use List::Util "sum";

%elements = (
  "foo"  => 1.213,
  "\0\n" => 0x12,
  ''     => -3,
  "bar"  => 1e-2
  ...
);
$sum = sum(map {$elements{$_}} (sort keys %elements)[-100 .. -1]);
#! /bin/zsh -

typeset -A elements
elements= (
  foo     1.213
  $'\0\n' 0x12
  ''      -3
  bar     1e-2
  ...
)
set -o extendedglob
sum=$(( ${(@j[ + ])${(@ko)elements}[-100,-1]/(#m)*/$elements[$MATCH]} ))

Those give you the sum of the elements for the last 100 keys sorted lexicographically (memcmp()-like comparison in perl, strcoll()-like in zsh).

For zsh, values may be any arithmetic expression recognised by zsh, but as we're eventually constructing a value1 + value2 + ... expression for zsh to evaluate, beware of precedence.

For instance, if value1 is var = 3 and value2 is 4 || hash[$(some-cmd)], that ends up evaluating var = 3 + 4 || hash[$(some-cmd)] (and some-cmd will be executed btw), so you may want to make sure values are surrounded with (...) if they're not just numbers.

bash associative arrays (for which you need a recent version) don't support arbitrary keys. It also has no operator for sorting lists (which you'd need for that last to make sense), and can't do floating point arithmetics.

Here, I'd use a less limited shell like zsh or a proper programming language like perl.

#! /usr/bin/perl
use List::Util "sum";

%elements = (
  "foo"  => 1.213,
  "\0\n" => 0x12,
  ''     => -3,
  "bar"  => 1e-2
  ...
);
$sum = sum(map {$elements{$_}} (sort keys %elements)[-100 .. -1]);
#! /bin/zsh -

typeset -A elements
elements= (
  foo     1.213
  $'\0\n' 0x12
  ''      -3
  bar     1e-2
  ...
)
set -o extendedglob
sum=$(( ${(@j[ + ])${(@ko)elements}[-100,-1]/(#m)*/$elements[$MATCH]} ))

Those give you the sum of the elements for the last 100 keys sorted lexicographically (memcmp()-like comparison in perl, strcoll()-like in zsh).

For zsh, values may be any arithmetic expression recognised by zsh, but as we're eventually constructing a value1 + value2 + ... expression for zsh to evaluate, beware of precedence.

For instance, if value1 is var = 3 and value2 is 4 || hash[$(some-cmd)], that ends up evaluating var = 3 + 4 || hash[$(some-cmd)] (and some-cmd will be executed btw), so you may want to make sure values are surrounded with (...) if they're not just numbers.

bash associative arrays (for which you need a recent version) don't support arbitrary keys. It also has no operator for sorting lists (which you'd need for that last to make sense), and can't do floating point arithmetics.

Here, I'd use a less limited shell like zsh or a proper programming language like perl.

#! /usr/bin/perl
use List::Util "sum";

%elements = (
  "foo"  => 1.213,
  "\0\n" => 0x12,
  ''     => -3,
  "bar"  => 1e-2
  ...
);
$sum = sum(map {$elements{$_}} (sort keys %elements)[-100 .. -1]);
#! /bin/zsh -

typeset -A elements
elements=(
  foo     1.213
  $'\0\n' 0x12
  ''      -3
  bar     1e-2
  ...
)
set -o extendedglob
sum=$(( ${(@j[ + ])${(@ko)elements}[-100,-1]/(#m)*/$elements[$MATCH]} ))

Those give you the sum of the elements for the last 100 keys sorted lexicographically (memcmp()-like comparison in perl, strcoll()-like in zsh).

For zsh, values may be any arithmetic expression recognised by zsh, but as we're eventually constructing a value1 + value2 + ... expression for zsh to evaluate, beware of precedence.

For instance, if value1 is var = 3 and value2 is 4 || hash[$(some-cmd)], that ends up evaluating var = 3 + 4 || hash[$(some-cmd)] (and some-cmd will be executed btw), so you may want to make sure values are surrounded with (...) if they're not just numbers.

added 465 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

bash associative arrays (for which you need a recent version) don't support arbitrary keys. It also has no operator for sorting lists (which you'd need for that last to make sense), and can't do floating point arithmetics.

Here, I'd use a less limited shell like zsh or a proper programming language like perl.

#! /usr/bin/perl
use List::Util "sum";

%elements = (
  "foo"  => 1.213,
  "\0\n" => 0x12,
  ''     => -3,
  "bar"  => 1e-2
  ...
);
$sum = sum(map {$elements{$_}} (sort keys %elements)[-100 .. -1]);
#! /bin/zsh -

typeset -A elements
elements= (
  foo     1.213
  $'\0\n' 0x12
  ''      -3
  bar     1e-2
  ...
)
set -o extendedglob
sum=$(( ${(@j[+]@j[ + ])${(@ko)elements}[-100,-1]/(#m)*/$elements[$MATCH]} ))

Those give you the sum of the elements for the last 100 keys sorted lexicographically (memcmp()-like comparison in perl, strcoll()-like in zsh).

For zsh, values may be any arithmetic expression recognised by zsh, but as we're eventually constructing a value1 + value2 + ... expression for zsh to evaluate, beware of precedence.

For instance, if value1 is var = 3 and value2 is 4 || hash[$(some-cmd)], that ends up evaluating var = 3 + 4 || hash[$(some-cmd)] (and some-cmd will be executed btw), so you may want to make sure values are surrounded with (...) if they're not just numbers.

bash associative arrays (for which you need a recent version) don't support arbitrary keys. It also has no operator for sorting lists (which you'd need for that last to make sense), and can't do floating point arithmetics.

Here, I'd use a less limited shell like zsh or a proper programming language like perl.

#! /usr/bin/perl
use List::Util "sum";

%elements = (
  "foo"  => 1.213,
  "\0\n" => 0x12,
  ''     => -3,
  "bar"  => 1e-2
  ...
);
$sum = sum(map {$elements{$_}} (sort keys %elements)[-100 .. -1]);
#! /bin/zsh -

typeset -A elements
elements= (
  foo     1.213
  $'\0\n' 0x12
  ''      -3
  bar     1e-2
  ...
)
set -o extendedglob
sum=$(( ${(@j[+])${(@ko)elements}[-100,-1]/(#m)*/$elements[$MATCH]} ))

Those give you the sum of the elements for the last 100 keys sorted lexicographically (memcmp()-like comparison in perl, strcoll()-like in zsh).

bash associative arrays (for which you need a recent version) don't support arbitrary keys. It also has no operator for sorting lists (which you'd need for that last to make sense), and can't do floating point arithmetics.

Here, I'd use a less limited shell like zsh or a proper programming language like perl.

#! /usr/bin/perl
use List::Util "sum";

%elements = (
  "foo"  => 1.213,
  "\0\n" => 0x12,
  ''     => -3,
  "bar"  => 1e-2
  ...
);
$sum = sum(map {$elements{$_}} (sort keys %elements)[-100 .. -1]);
#! /bin/zsh -

typeset -A elements
elements= (
  foo     1.213
  $'\0\n' 0x12
  ''      -3
  bar     1e-2
  ...
)
set -o extendedglob
sum=$(( ${(@j[ + ])${(@ko)elements}[-100,-1]/(#m)*/$elements[$MATCH]} ))

Those give you the sum of the elements for the last 100 keys sorted lexicographically (memcmp()-like comparison in perl, strcoll()-like in zsh).

For zsh, values may be any arithmetic expression recognised by zsh, but as we're eventually constructing a value1 + value2 + ... expression for zsh to evaluate, beware of precedence.

For instance, if value1 is var = 3 and value2 is 4 || hash[$(some-cmd)], that ends up evaluating var = 3 + 4 || hash[$(some-cmd)] (and some-cmd will be executed btw), so you may want to make sure values are surrounded with (...) if they're not just numbers.

added 3 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

bash associative arrays (for which you need a recent version) don't support arbitrary keys. It also has no operator for sorting lists (which you'd need for that last to make sense), and can't do floating point arithmetics.

Here, I'd use a less limited shell like zsh or a proper programming language like perl.

#! /usr/bin/perl
use List::Util=sum;Util "sum";

%elements = (
  "foo"  => 1.213,
  "\0\n" => 0x12,
  ''     => -3,
  "bar"  => 1e-2
  ...
);
$sum = sum(map {$elements{$_}} (sort keys %elements)[-100 .. -1];1]);
#! /bin/zsh -

typeset -A elements
elements= (
  foo     1.213
  $'\0\n' 0x12
  ''      -3
  bar     1e-2
  ...
)
set -o extendedglob
sum=$(( ${(@j[+])${(@ko)elements}[-100,-1]/(#m)*/$elements[$MATCH]} ))

Those give you the sum of the elements for the last 100 keys sorted lexicographically (memcmp()-like comparison in perl, strcoll()-like in zsh).

bash associative arrays (for which you need a recent version) don't support arbitrary keys. It also has no operator for sorting lists (which you'd need for that last to make sense), and can't do floating point arithmetics.

Here, I'd use a less limited shell like zsh or a proper programming language like perl.

#! /usr/bin/perl
use List::Util=sum;

%elements = (
  "foo"  => 1.213,
  "\0\n" => 0x12,
  ''     => -3,
  "bar"  => 1e-2
  ...
);
$sum = sum(map {$elements{$_}} (sort keys %elements)[-100 .. -1];
#! /bin/zsh -

typeset -A elements
elements= (
  foo     1.213
  $'\0\n' 0x12
  ''      -3
  bar     1e-2
  ...
)
set -o extendedglob
sum=$(( ${(@j[+])${(@ko)elements}[-100,-1]/(#m)*/$elements[$MATCH]} ))

Those give you the sum of the elements for the last 100 keys sorted lexicographically (memcmp()-like comparison in perl, strcoll()-like in zsh).

bash associative arrays (for which you need a recent version) don't support arbitrary keys. It also has no operator for sorting lists (which you'd need for that last to make sense), and can't do floating point arithmetics.

Here, I'd use a less limited shell like zsh or a proper programming language like perl.

#! /usr/bin/perl
use List::Util "sum";

%elements = (
  "foo"  => 1.213,
  "\0\n" => 0x12,
  ''     => -3,
  "bar"  => 1e-2
  ...
);
$sum = sum(map {$elements{$_}} (sort keys %elements)[-100 .. -1]);
#! /bin/zsh -

typeset -A elements
elements= (
  foo     1.213
  $'\0\n' 0x12
  ''      -3
  bar     1e-2
  ...
)
set -o extendedglob
sum=$(( ${(@j[+])${(@ko)elements}[-100,-1]/(#m)*/$elements[$MATCH]} ))

Those give you the sum of the elements for the last 100 keys sorted lexicographically (memcmp()-like comparison in perl, strcoll()-like in zsh).

Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k
Loading