Find adjacents in bidimensional array - ruby

I have a 2d array like this.
[
["+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+"],
["|", "*", "*", " ", " ", "*", " ", " ", " ", " ", "*", "*", "*", "*", "*", "*", "*", "*", " ", "*", " ", "|"],
["|", " ", "*", "*", "*", "*", " ", " ", " ", " ", "*", "*", " ", " ", "*", "*", " ", "*", " ", "*", " ", "|"],
["|", "*", " ", "*", " ", " ", " ", " ", "*", " ", "*", " ", "*", "*", "*", " ", "*", "*", "*", " ", "*", "|"],
["|", "*", "*", " ", " ", "*", " ", " ", "*", "*", "*", " ", "*", " ", "*", "*", " ", "*", "*", " ", " ", "|"],
["|", "*", " ", " ", " ", "*", " ", "*", " ", " ", " ", "*", "*", "*", "*", "*", " ", " ", " ", "*", "*", "|"],
["|", " ", " ", " ", " ", " ", "*", " ", "*", " ", " ", " ", "*", " ", " ", " ", " ", " ", "*", " ", " ", "|"],
["|", "*", "*", " ", "*", "*", "*", " ", "*", " ", " ", "*", "*", "*", "*", " ", "*", " ", " ", "*", " ", "|"],
["|", " ", "*", " ", "*", "*", "*", "*", " ", " ", " ", "*", "*", " ", " ", " ", " ", "*", " ", "*", "*", "|"],
["|", " ", "*", " ", " ", "*", " ", " ", "*", " ", "*", "*", "*", " ", "*", " ", "*", " ", " ", " ", "*", "|"],
["|", " ", "*", "*", "*", "*", " ", " ", " ", " ", "*", "*", " ", "*", "*", "*", " ", " ", " ", "*", "*", "|"],
["|", " ", " ", "*", "*", " ", "*", "*", "*", " ", " ", " ", " ", " ", " ", "*", "*", " ", " ", " ", " ", "|"],
["|", "*", " ", "*", "*", " ", "*", " ", "*", "*", " ", " ", " ", "*", " ", "*", "*", "*", " ", " ", " ", "|"],
["|", " ", " ", " ", "*", " ", "*", " ", "*", " ", "*", " ", " ", " ", " ", " ", " ", "*", "*", " ", " ", "|"],
["|", "*", " ", "*", " ", "*", " ", "*", " ", " ", " ", "*", " ", "*", "*", " ", "*", "*", "*", "*", "*", "|"],
["|", " ", " ", " ", "*", " ", "*", "*", "*", "*", "*", "*", " ", " ", "*", " ", " ", "*", "*", " ", "*", "|"],
["|", "*", "*", "*", "*", "*", " ", " ", " ", "*", " ", "*", " ", "*", "*", " ", "*", "*", " ", " ", "*", "|"],
["|", "*", " ", " ", " ", "*", "*", "*", "*", " ", " ", "*", " ", "*", " ", "*", " ", "*", " ", "*", "*", "|"],
["|", "*", " ", "*", "*", "*", "*", "*", "*", " ", "*", "*", "*", "*", " ", " ", " ", "*", " ", " ", " ", "|"],
["|", " ", " ", "*", " ", " ", "*", "*", " ", " ", " ", "*", " ", "*", "*", " ", " ", " ", " ", " ", "*", "|"],
["|", " ", "*", " ", " ", " ", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", " ", " ", " ", "*", "|"],
["+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+"]
]
and i need to find the number of bombs that are near each "*" as shown in the image below.
Im doing this to get indexes but i dont know how to get the adjacents.
#array_map.each_index do |i|
subarray = #array_map[i]
subarray.each_index do |x|
puts String(i) << " " << String(x) << "... " << #array_map[i][x]
end
end

minefield = [
["+", "-", "-", "-", "-", "-", "-", "-"],
["|", "*", "*", " ", " ", "*", " ", " "],
["|", " ", "*", "*", "*", "*", " ", " "],
["|", "*", " ", "*", " ", " ", " ", " "],
["|", "*", "*", " ", " ", "*", " ", " "],
["|", "*", " ", " ", " ", "*", " ", "*"],
["|", " ", " ", " ", " ", " ", "*", " "]
]
last_row = minefield.size-1
#=> 6
last_col = minefield.first.size-1
#=> 7
adj_cols = (0..last_col).each_with_object({}) do |j,h|
h[j] = ([j-1, 0].max..[j+1, last_col].min).to_a
end
#=> {0=>[0, 1], 1=>[0, 1, 2], 2=>[1, 2, 3], 3=>[2, 3, 4], 4=>[3, 4, 5],
# 5=>[4, 5, 6], 6=>[5, 6, 7], 7=>[6, 7]}
arr = (0..last_row).each_with_object(minefield.dup.map(&:dup)) do |i,a|
adj_rows = ([i-1, 0].max..[i+1, last_row].min).to_a
(0..last_col).each do |j|
next unless a[i][j] == ' '
a[i][j] = adj_rows.product(adj_cols[j]).count do |r,c|
minefield[r][c] == '*'
end.to_s
end
end
arr.each { |row| p row }
displays
["+", "-", "-", "-", "-", "-", "-", "-"]
["|", "*", "*", "4", "4", "*", "2", "0"]
["|", "4", "*", "*", "*", "*", "2", "0"]
["|", "*", "6", "*", "5", "3", "2", "0"]
["|", "*", "*", "2", "3", "*", "3", "1"]
["|", "*", "3", "1", "2", "*", "4", "*"]
["|", "1", "1", "0", "1", "2", "*", "2"]
See Array#product. adj_cols is a hash that gives an array of column indices to identify adjacent positions for each column index. It makes sense to do this once at the beginning rather than repeat the calculation for each element (row) of minefield.
The array to be returned is initialized to
minefield.dup.map(&:dup)
so that minefield will not be mutated (modified).
Here's an example calculation.
i = 3
adj_rows = ([i-1, 0].max..[i+1, last_row].min).to_a
#=> ([2, 0].max..[4, 6].min).to_a
#=> (2..4).to_a
#=> [2, 3, 4]
j = 4
a = adj_cols[j]
#=> [3, 4, 5]
b = adj_rows.product(a)
#=> [[2, 3], [2, 4], [2, 5],
# [3, 3], [3, 4], [3, 5],
# [4, 3], [4, 4], [4, 5]]
b.count { |r,c| minefield[r][c] == '*' }
#=> 5
Note that b includes [3, 4], which we know equals ' '. It does no harm to leave it in the count operation, however, as minefield[r][c] == '*' #=> false). We could alternatively write:
b = adj_rows.product(a) - [[i, j]]
#=> [[2, 3], [2, 4], [2, 5],
# [3, 3], [3, 5],
# [4, 3], [4, 4], [4, 5]]
Here's a second example (that would apply if minefield[0][4] #=> ' ').
i = 0
j = 4
adj_rows = ([i-1, 0].max..[i+1, last_row].min).to_a
#=> [0, 1]
j = 4
a = adj_cols[j]
#=> [3, 4, 5]
b = adj_rows.product(a)
#=> [[0, 3], [0, 4], [0, 5],
# [1, 3], [1, 4], [1, 5]]
For the minefield array given in the question the return value is as follows.
["+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+"]
["|", "*", "*", "4", "4", "*", "2", "0", "0", "2", "*", "*", "*", "*", "*", "*", "*", "*", "4", "*", "2", "|"]
["|", "4", "*", "*", "*", "*", "2", "1", "1", "4", "*", "*", "6", "7", "*", "*", "7", "*", "6", "*", "3", "|"]
["|", "*", "6", "*", "5", "3", "2", "2", "*", "6", "*", "6", "*", "*", "*", "6", "*", "*", "*", "4", "*", "|"]
["|", "*", "*", "2", "3", "*", "3", "3", "*", "*", "*", "6", "*", "8", "*", "*", "5", "*", "*", "5", "3", "|"]
["|", "*", "3", "1", "2", "*", "4", "*", "4", "4", "3", "*", "*", "*", "*", "*", "3", "3", "4", "*", "*", "|"]
["|", "3", "3", "2", "3", "5", "*", "5", "*", "2", "2", "5", "*", "7", "5", "4", "2", "2", "*", "4", "3", "|"]
["|", "*", "*", "4", "*", "*", "*", "6", "*", "2", "2", "*", "*", "*", "*", "2", "*", "3", "4", "*", "3", "|"]
["|", "4", "*", "5", "*", "*", "*", "*", "3", "3", "4", "*", "*", "6", "3", "4", "3", "*", "3", "*", "*", "|"]
["|", "3", "*", "6", "6", "*", "5", "3", "*", "3", "*", "*", "*", "5", "*", "4", "*", "2", "3", "5", "*", "|"]
["|", "2", "*", "*", "*", "*", "4", "4", "3", "4", "*", "*", "4", "*", "*", "*", "4", "2", "1", "*", "*", "|"]
["|", "2", "5", "*", "*", "6", "*", "*", "*", "4", "3", "2", "3", "3", "6", "*", "*", "3", "2", "2", "2", "|"]
["|", "*", "3", "*", "*", "6", "*", "7", "*", "*", "2", "1", "1", "*", "3", "*", "*", "*", "3", "1", "0", "|"]
["|", "2", "4", "4", "*", "5", "*", "5", "*", "4", "*", "2", "3", "3", "4", "4", "6", "*", "*", "4", "2", "|"]
["|", "*", "2", "*", "4", "*", "5", "*", "5", "5", "5", "*", "3", "*", "*", "3", "*", "*", "*", "*", "*", "|"]
["|", "3", "5", "5", "*", "5", "*", "*", "*", "*", "*", "*", "5", "5", "*", "5", "5", "*", "*", "6", "*", "|"]
["|", "*", "*", "*", "*", "*", "6", "6", "6", "*", "6", "*", "5", "*", "*", "4", "*", "*", "5", "5", "*", "|"]
["|", "*", "6", "5", "7", "*", "*", "*", "*", "4", "5", "*", "7", "*", "5", "*", "5", "*", "4", "*", "*", "|"]
["|", "*", "4", "*", "*", "*", "*", "*", "*", "3", "*", "*", "*", "*", "5", "2", "3", "*", "3", "3", "3", "|"]
["|", "2", "4", "*", "4", "5", "*", "*", "6", "5", "6", "*", "8", "*", "*", "4", "3", "2", "1", "2", "*", "|"]
["|", "1", "*", "2", "1", "2", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "1", "0", "2", "*", "|"]
["+", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "+"]

This would work:
minefield = [
["+", "-", "-", "-", "-", "-", "+"],
["|", "*", "*", "*", "*", "*", "|"],
["|", "*", " ", "*", "*", " ", "|"],
["|", " ", " ", " ", "*", " ", "|"],
["|", "*", " ", "*", "*", "*", "|"],
["|", "*", "*", "*", " ", " ", "|"],
["+", "-", "-", "-", "-", "-", "+"]
]
minefield.each_with_index do |row, y|
row.each_with_index do |item, x|
next unless item == '*'
(y - 1).upto(y + 1) do |j|
(x - 1).upto(x + 1) do |i|
next if minefield[j][i] =~ /[*|+-]/
minefield[j][i] = minefield[j][i].to_i.succ.to_s
end
end
end
end
It traverses the minefield array, skipping everything but bombs (next unless item == '*'). Once we encounter a bomb, we traverse its surroundings, skipping any special fields like *, |, +, or -. (this will also skip the bomb itself)
And since bombs don't occur on the minefield's border, we can omit bounds checking.
For the remaining surrounding fields (i.e. blanks and numeric strings), we convert that field to integer (so " " becomes 0), add 1 (via succ), convert it back to string and re-assign it to the field: (succ also works on strings, but we have to handle " " anyway)
# blank # numeric
minefield[j][i] #=> " " # "1"
.to_i #=> 0 # 1
.succ #=> 1 # 2
.to_s #=> "1" # "2"
In the end, the blank fields have been replaced by the bomb counts:
[
["+", "-", "-", "-", "-", "-", "+"],
["|", "*", "*", "*", "*", "*", "|"],
["|", "*", "5", "*", "*", "4", "|"],
["|", "2", "4", "5", "*", "4", "|"],
["|", "*", "5", "*", "*", "*", "|"],
["|", "*", "*", "*", "4", "2", "|"],
["+", "-", "-", "-", "-", "-", "+"]
]

Related

Magento 2 update dont start "rejected by your constraint"

I cant update my Magento 2.3.6 Installation. composer update responds with
composer update
Loading composer repositories with package information
Warning from https://repo.packagist.org: You are using an outdated version of Composer. Composer 2 is now available and you should upgrade. See https://getcomposer.org/2
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
The requested package magento/product-community-edition ^2.4.0 exists as magento/product-community-edition[2.3.6] but these are rejected by your constraint.
I tried several variants ("2.4.2", "^2.4", "~2.4") of demanding 2.4.2 and every variant had the same result. On which constraint does the Error refer?
This is my composer.json
{
"name": "magento/magento2ce",
"description": "Magento 2 (Open Source)",
"type": "project",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"require": {
"clever/magento2": "2.4.2",
"magenerds/germanlaw": "1.2.1.1",
"magenerds/language-de_de": "^1.0",
"magento/product-community-edition": "^2.4.0",
"mageplaza/module-core": "^1.4",
"mageworx/module-giftcards": "^2.8",
"payone-gmbh/magento-2": "^2.7",
"trustedshops/trustedshops": "1.0.4",
"php": "~7.3.0||~7.4.0",
"ext-bcmath": "*",
"ext-ctype": "*",
"ext-curl": "*",
"ext-dom": "*",
"ext-gd": "*",
"ext-hash": "*",
"ext-iconv": "*",
"ext-intl": "*",
"ext-mbstring": "*",
"ext-openssl": "*",
"ext-pdo_mysql": "*",
"ext-simplexml": "*",
"ext-soap": "*",
"ext-xsl": "*",
"ext-zip": "*",
"lib-libxml": "*",
"colinmollenhour/cache-backend-file": "~1.4.1",
"colinmollenhour/cache-backend-redis": "~1.10",
"colinmollenhour/credis": "~1.10",
"colinmollenhour/php-redis-session-abstract": "~1.4.0",
"composer/composer": "^1.9 || ^2.0",
"elasticsearch/elasticsearch": "~7.11.0",
"guzzlehttp/guzzle": "^6.3.3",
"laminas/laminas-captcha": "^2.10",
"laminas/laminas-code": "~3.3",
"laminas/laminas-crypt": "~3.3",
"laminas/laminas-db": "^2.12.0",
"laminas/laminas-dependency-plugin": "^2.1.0",
"laminas/laminas-di": "^3.2.0",
"laminas/laminas-eventmanager": "^3.0.0",
"laminas/laminas-feed": "^2.13.0",
"laminas/laminas-filter": "^2.11",
"laminas/laminas-http": "^2.6.0",
"laminas/laminas-i18n": "^2.7.3",
"laminas/laminas-json": "^3.2.0",
"laminas/laminas-mail": "^2.9.0",
"laminas/laminas-mime": "^2.8.0",
"laminas/laminas-modulemanager": "^2.7",
"laminas/laminas-mvc": "^3.2.0",
"laminas/laminas-server": "^2.6.1",
"laminas/laminas-servicemanager": "^3.6.0",
"laminas/laminas-session": "^2.10",
"laminas/laminas-soap": "^2.9.0",
"laminas/laminas-stdlib": "^3.2.1",
"laminas/laminas-text": "^2.6.0",
"laminas/laminas-uri": "^2.5.1",
"laminas/laminas-validator": "^2.6.0",
"laminas/laminas-view": "~2.12.0",
"league/flysystem": "^2.0",
"league/flysystem-aws-s3-v3": "^2.0",
"magento/composer": "1.6.0",
"magento/magento-composer-installer": ">=0.1.11",
"magento/zendframework1": "~1.14.2",
"monolog/monolog": "^1.17",
"paragonie/sodium_compat": "^1.6",
"pelago/emogrifier": "^5.0.0",
"php-amqplib/php-amqplib": "~2.10.0",
"phpseclib/mcrypt_compat": "1.0.8",
"phpseclib/phpseclib": "2.0.*",
"ramsey/uuid": "~3.8.0",
"symfony/console": "~4.4.0",
"symfony/event-dispatcher": "~4.4.0",
"symfony/process": "~4.4.0",
"tedivm/jshrink": "~1.4.0",
"tubalmartin/cssmin": "4.1.1",
"webonyx/graphql-php": "^0.13.8",
"wikimedia/less.php": "^3.0.0",
"web-token/jwt-framework": "^v2.2.7"
},
"require-dev": {
"allure-framework/allure-phpunit": "~1.2.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"friendsofphp/php-cs-fixer": "~2.18.1",
"lusitanian/oauth": "~0.8.10",
"magento/magento-coding-standard": "*",
"magento/magento2-functional-testing-framework": "^3.0",
"pdepend/pdepend": "~2.7.1",
"phpcompatibility/php-compatibility": "^9.3",
"phpmd/phpmd": "^2.8.0",
"phpstan/phpstan": "^0.12.77",
"phpunit/phpunit": "^9",
"sebastian/phpcpd": "~5.0.0",
"squizlabs/php_codesniffer": "~3.5.4"
},
"suggest": {
"ext-pcntl": "Need for run processes in parallel mode"
},
"replace": {
"magento/module-marketplace": "*",
"magento/module-admin-analytics": "*",
"magento/module-admin-notification": "*",
"magento/module-advanced-pricing-import-export": "*",
"magento/module-amqp": "*",
"magento/module-amqp-store": "*",
"magento/module-analytics": "*",
"magento/module-asynchronous-operations": "*",
"magento/module-authorization": "*",
"magento/module-advanced-search": "*",
"magento/module-backend": "*",
"magento/module-backup": "*",
"magento/module-bundle": "*",
"magento/module-bundle-graph-ql": "*",
"magento/module-bundle-import-export": "*",
"magento/module-cache-invalidate": "*",
"magento/module-captcha": "*",
"magento/module-cardinal-commerce": "*",
"magento/module-catalog": "*",
"magento/module-catalog-customer-graph-ql": "*",
"magento/module-catalog-analytics": "*",
"magento/module-catalog-import-export": "*",
"magento/module-catalog-inventory": "*",
"magento/module-catalog-inventory-graph-ql": "*",
"magento/module-catalog-rule": "*",
"magento/module-catalog-rule-graph-ql": "*",
"magento/module-catalog-rule-configurable": "*",
"magento/module-catalog-search": "*",
"magento/module-catalog-url-rewrite": "*",
"magento/module-catalog-widget": "*",
"magento/module-checkout": "*",
"magento/module-checkout-agreements": "*",
"magento/module-checkout-agreements-graph-ql": "*",
"magento/module-cms": "*",
"magento/module-cms-url-rewrite": "*",
"magento/module-compare-list-graph-ql": "*",
"magento/module-config": "*",
"magento/module-configurable-import-export": "*",
"magento/module-configurable-product": "*",
"magento/module-configurable-product-sales": "*",
"magento/module-contact": "*",
"magento/module-cookie": "*",
"magento/module-cron": "*",
"magento/module-currency-symbol": "*",
"magento/module-customer": "*",
"magento/module-customer-analytics": "*",
"magento/module-customer-downloadable-graph-ql": "*",
"magento/module-customer-import-export": "*",
"magento/module-deploy": "*",
"magento/module-developer": "*",
"magento/module-dhl": "*",
"magento/module-directory": "*",
"magento/module-directory-graph-ql": "*",
"magento/module-downloadable": "*",
"magento/module-downloadable-graph-ql": "*",
"magento/module-downloadable-import-export": "*",
"magento/module-eav": "*",
"magento/module-elasticsearch": "*",
"magento/module-elasticsearch-6": "*",
"magento/module-elasticsearch-7": "*",
"magento/module-email": "*",
"magento/module-encryption-key": "*",
"magento/module-fedex": "*",
"magento/module-gift-message": "*",
"magento/module-gift-message-graph-ql": "*",
"magento/module-google-adwords": "*",
"magento/module-google-analytics": "*",
"magento/module-google-optimizer": "*",
"magento/module-graph-ql": "*",
"magento/module-graph-ql-cache": "*",
"magento/module-catalog-graph-ql": "*",
"magento/module-catalog-cms-graph-ql": "*",
"magento/module-catalog-url-rewrite-graph-ql": "*",
"magento/module-configurable-product-graph-ql": "*",
"magento/module-customer-graph-ql": "*",
"magento/module-eav-graph-ql": "*",
"magento/module-swatches-graph-ql": "*",
"magento/module-tax-graph-ql": "*",
"magento/module-url-rewrite-graph-ql": "*",
"magento/module-cms-url-rewrite-graph-ql": "*",
"magento/module-weee-graph-ql": "*",
"magento/module-cms-graph-ql": "*",
"magento/module-grouped-import-export": "*",
"magento/module-grouped-product": "*",
"magento/module-grouped-catalog-inventory": "*",
"magento/module-grouped-product-graph-ql": "*",
"magento/module-import-export": "*",
"magento/module-indexer": "*",
"magento/module-instant-purchase": "*",
"magento/module-integration": "*",
"magento/module-layered-navigation": "*",
"magento/module-login-as-customer": "*",
"magento/module-login-as-customer-admin-ui": "*",
"magento/module-login-as-customer-api": "*",
"magento/module-login-as-customer-assistance": "*",
"magento/module-login-as-customer-frontend-ui": "*",
"magento/module-login-as-customer-graph-ql": "*",
"magento/module-login-as-customer-log": "*",
"magento/module-login-as-customer-quote": "*",
"magento/module-login-as-customer-page-cache": "*",
"magento/module-login-as-customer-sales": "*",
"magento/module-media-content": "*",
"magento/module-media-content-api": "*",
"magento/module-media-content-catalog": "*",
"magento/module-media-content-cms": "*",
"magento/module-media-gallery": "*",
"magento/module-media-gallery-api": "*",
"magento/module-media-gallery-ui": "*",
"magento/module-media-gallery-ui-api": "*",
"magento/module-media-gallery-integration": "*",
"magento/module-media-gallery-synchronization": "*",
"magento/module-media-gallery-synchronization-api": "*",
"magento/module-media-content-synchronization": "*",
"magento/module-media-content-synchronization-api": "*",
"magento/module-media-content-synchronization-catalog": "*",
"magento/module-media-content-synchronization-cms": "*",
"magento/module-media-gallery-synchronization-metadata": "*",
"magento/module-media-gallery-metadata": "*",
"magento/module-media-gallery-metadata-api": "*",
"magento/module-media-gallery-catalog-ui": "*",
"magento/module-media-gallery-cms-ui": "*",
"magento/module-media-gallery-catalog-integration": "*",
"magento/module-media-gallery-catalog": "*",
"magento/module-media-gallery-renditions": "*",
"magento/module-media-gallery-renditions-api": "*",
"magento/module-media-storage": "*",
"magento/module-message-queue": "*",
"magento/module-msrp": "*",
"magento/module-msrp-configurable-product": "*",
"magento/module-msrp-grouped-product": "*",
"magento/module-multishipping": "*",
"magento/module-mysql-mq": "*",
"magento/module-new-relic-reporting": "*",
"magento/module-newsletter": "*",
"magento/module-newsletter-graph-ql": "*",
"magento/module-offline-payments": "*",
"magento/module-offline-shipping": "*",
"magento/module-page-cache": "*",
"magento/module-payment": "*",
"magento/module-paypal": "*",
"magento/module-paypal-captcha": "*",
"magento/module-paypal-graph-ql": "*",
"magento/module-persistent": "*",
"magento/module-product-alert": "*",
"magento/module-product-video": "*",
"magento/module-quote": "*",
"magento/module-quote-analytics": "*",
"magento/module-quote-bundle-options": "*",
"magento/module-quote-configurable-options": "*",
"magento/module-quote-downloadable-links": "*",
"magento/module-quote-graph-ql": "*",
"magento/module-related-product-graph-ql": "*",
"magento/module-release-notification": "*",
"magento/module-reports": "*",
"magento/module-require-js": "*",
"magento/module-review": "*",
"magento/module-review-graph-ql": "*",
"magento/module-review-analytics": "*",
"magento/module-robots": "*",
"magento/module-rss": "*",
"magento/module-rule": "*",
"magento/module-sales": "*",
"magento/module-sales-analytics": "*",
"magento/module-sales-graph-ql": "*",
"magento/module-sales-inventory": "*",
"magento/module-sales-rule": "*",
"magento/module-sales-sequence": "*",
"magento/module-sample-data": "*",
"magento/module-search": "*",
"magento/module-security": "*",
"magento/module-send-friend": "*",
"magento/module-send-friend-graph-ql": "*",
"magento/module-shipping": "*",
"magento/module-sitemap": "*",
"magento/module-store": "*",
"magento/module-store-graph-ql": "*",
"magento/module-swagger": "*",
"magento/module-swagger-webapi": "*",
"magento/module-swagger-webapi-async": "*",
"magento/module-swatches": "*",
"magento/module-swatches-layered-navigation": "*",
"magento/module-tax": "*",
"magento/module-tax-import-export": "*",
"magento/module-theme": "*",
"magento/module-theme-graph-ql": "*",
"magento/module-translation": "*",
"magento/module-ui": "*",
"magento/module-ups": "*",
"magento/module-url-rewrite": "*",
"magento/module-user": "*",
"magento/module-usps": "*",
"magento/module-variable": "*",
"magento/module-vault": "*",
"magento/module-vault-graph-ql": "*",
"magento/module-version": "*",
"magento/module-webapi": "*",
"magento/module-webapi-async": "*",
"magento/module-webapi-security": "*",
"magento/module-weee": "*",
"magento/module-widget": "*",
"magento/module-wishlist": "*",
"magento/module-wishlist-graph-ql": "*",
"magento/module-wishlist-analytics": "*",
"magento/theme-adminhtml-backend": "*",
"magento/theme-frontend-blank": "*",
"magento/theme-frontend-luma": "*",
"magento/language-de_de": "*",
"magento/language-en_us": "*",
"magento/language-es_es": "*",
"magento/language-fr_fr": "*",
"magento/language-nl_nl": "*",
"magento/language-pt_br": "*",
"magento/language-zh_hans_cn": "*",
"magento/framework": "*",
"magento/framework-amqp": "*",
"magento/framework-bulk": "*",
"magento/framework-message-queue": "*",
"trentrichardson/jquery-timepicker-addon": "1.4.3",
"components/jquery": "1.11.0",
"blueimp/jquery-file-upload": "5.6.14",
"components/jqueryui": "1.10.4",
"twbs/bootstrap": "3.1.0",
"tinymce/tinymce": "3.4.7",
"magento/module-tinymce-3": "*",
"magento/module-csp": "*",
"magento/module-aws-s3": "*",
"magento/module-remote-storage": "*",
"magento/module-jwt-framework-adapter": "*"
},
"conflict": {
"gene/bluefoot": "*"
},
"extra": {
"component_paths": {
"trentrichardson/jquery-timepicker-addon": "lib/web/jquery/jquery-ui-timepicker-addon.js",
"components/jquery": [
"lib/web/jquery.js",
"lib/web/jquery/jquery.min.js",
"lib/web/jquery/jquery-migrate.js"
],
"blueimp/jquery-file-upload": "lib/web/jquery/fileUploader",
"components/jqueryui": [
"lib/web/jquery/jquery-ui.js"
],
"twbs/bootstrap": [
"lib/web/jquery/jquery.tabs.js"
],
"tinymce/tinymce": "lib/web/tiny_mce_4"
}
},
"autoload": {
"psr-4": {
"Magento\\Framework\\": "lib/internal/Magento/Framework/",
"Magento\\Setup\\": "setup/src/Magento/Setup/",
"Magento\\": "app/code/Magento/"
},
"psr-0": {
"": [
"app/code/",
"generated/code/"
]
},
"files": [
"app/etc/NonComposerComponentRegistration.php"
],
"exclude-from-classmap": [
"**/dev/**",
"**/update/**",
"**/Test/**"
]
},
"autoload-dev": {
"psr-4": {
"Magento\\Sniffs\\": "dev/tests/static/framework/Magento/Sniffs/",
"Magento\\Tools\\": "dev/tools/Magento/Tools/",
"Magento\\Tools\\Sanity\\": "dev/build/publication/sanity/Magento/Tools/Sanity/",
"Magento\\TestFramework\\Inspection\\": "dev/tests/static/framework/Magento/TestFramework/Inspection/",
"Magento\\TestFramework\\Utility\\": "dev/tests/static/framework/Magento/TestFramework/Utility/",
"Magento\\PhpStan\\": "dev/tests/static/framework/Magento/PhpStan/"
}
},
"prefer-stable": true
}
What is the Problem here?
Upgrade your composer to the latest version and update the specified packages to match your environment

Ruby string to array conversion using 'scan' function

I have a string:
"10/2+3*3/2-3/2+10"
I need to convert it to an array:
["10", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "10"]
without using any gem.
I am using the following code to try to convert it:
"10/2+3*3/2-3/2+10".scan(/[\d*,+,\-,*,\/]/)
# => ["1", "0", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "1", "0"]
The output is not what I was expecting.
Use String#split
"10/2+3*3/2-3/2+10".split(/(\D)/)
#=> ["10", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "10"]
Commas inside square brackets in regular expression are treated as commas. What you want is the union of patterns.
"10/2+3*3/2-3/2+10".scan(/\d+|\+|\*|\/|\-/)
"10/2+3*3/2-3/2+10".scan(/\d+|./)
#=> ["10", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "10"]

D3 Line Chart not populating; what's missing?

I am trying to sum up values in a csv and populate it using a line chart (Current team strength across months). Now, I am not getting any errors, & neither am getting the chart populated. Can't seem to figure out what is missing.
Here is my code:
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
body { font: 12px Arial;}
.line {
stroke: blue;
fill:none;
stroke-width: 4;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-size: 10px;
font-family: sans-serif;
}
.text-label {
font-size: 10px;
font-family: sans-serif;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.time.format("%m/%d/%Y").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
d3.csv("Test.csv", function(error, data) {
if (error) throw error;
// Not yet using filtering
var filter = data.filter(function(d){
return (d.Head == 'People' && d.Measure == 'Current Team')
});
var nested = d3.nest()
.key(function(d) {return d.Time_Period;})
.rollup(function(d) {
return {
line1: d3.sum(d, function(e) { return e.Value; })
};
//console.log(line1);
})
.entries(data);
console.log(nested);
x.domain(d3.extent(nested, function(d) { return d.key; }));
y.domain(d3.extent(nested, function(d) { return d.values.line1; }));
// Adds the svg canvas
var svg = d3.select("body").append("svg");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
var line_1 = d3.svg.line()
.x(function(d) { console.log(parseDate(d.key));return parseDate(d.key); })
.y(function(d) { console.log(d.values.line1);return d.values.line1; });
console.log(line_1.x.value);
svg.append("path")
.datum(nested)
.attr("class", "line")
.attr("d", line_1)
.style("stroke", "steelblue")
.attr("fill", "none")
.attr("stroke-width", 4.8)
.attr("stroke-opacity", 0.0001)
.transition().duration(2000)
.attr("stroke-opacity", 1)
.attr("stroke-width", 2.8);
});
</script>
<Test.csv>
[
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "Current Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "Current Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "New Joinees",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "New Joinees",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "Current Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "Current Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "New Joinees",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "New Joinees",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "Current Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "Current Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "New Joinees",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "New Joinees",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "Current Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "Current Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "New Joinees",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "New Joinees",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
},
{
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
}
]
I made following updates in your code and it seems to be working fine in the snippet.
Position and size of SVG container.
Parsing of x axis date values.
The values in your question was showing a straight line. I have used some random values for a different look.
var data = [{
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "Current Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "Current Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "New Joinees",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "75",
"Measure": "New Joinees",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "18",
"Measure": "Planned Team",
"Time_Period": "4/1/2016",
"Unit": "Cunt"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "15",
"Measure": "Planned Team",
"Time_Period": "4/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "Current Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "Current Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "New Joinees",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "New Joinees",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "5/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "Current Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "Current Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "New Joinees",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "New Joinees",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "6/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "Current Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "Current Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Current Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "6",
"Measure": "New Joinees",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "5",
"Measure": "New Joinees",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "New Joinees",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "10",
"Measure": "Planned Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "IN",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "20",
"Measure": "Planned Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Reporting & Vizualization",
"Value": "30",
"Measure": "Planned Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
}, {
"RU": "US",
"Head": "People",
"Industry": "-",
"Practice": "Data Integration",
"Value": "0",
"Measure": "Planned Team",
"Time_Period": "7/1/2016",
"Unit": "Count"
}];
var margin = {
top: 30,
right: 20,
bottom: 30,
left: 50
},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.time.format("%m/%d/%Y").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Not yet using filtering
var filter = data.filter(function(d) {
return (d.Head == 'People' && d.Measure == 'Current Team')
});
var nested = d3.nest().key(function(d) {
return d.Time_Period;
})
.rollup(function(d) {
return d3.sum(d, function(e) {
return +e.Value;
})
//console.log(line1);
})
.entries(data);
x.domain(d3.extent(nested, function(d) {
return parseDate(d.key);
}));
y.domain(d3.extent(nested, function(d) {
return d.values;
}));
// Adds the svg canvas
var svg = d3.select("body").append("svg").attr("height", height + margin.top + margin.bottom)
.attr("width", width + margin.left + margin.right)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
var line_1 = d3.svg.line()
.x(function(d) {
return x(parseDate(d.key));
})
.y(function(d) {
return d.values;
});
//console.log(line_1.x.value);
svg.append("path")
.datum(nested)
.attr("class", "line")
.attr("d", line_1)
.style("stroke", "steelblue")
.attr("fill", "none")
.attr("stroke-width", 4.8)
.attr("stroke-opacity", 0.0001)
.transition().duration(2000)
.attr("stroke-opacity", 1)
.attr("stroke-width", 2.8);
.line {
stroke: blue;
fill: none;
stroke-width: 4;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-size: 10px;
font-family: sans-serif;
}
.text-label {
font-size: 10px;
font-family: sans-serif;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

gets.chomp find a string in a list

Is it possible to find a string in a list for a gets.chomp for if, elsif?
def number_problem()
puts "Please enter a number 1-100 for a phrase or word back!"
number = gets.chomp
mult_of_three = ["3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "39", "42", "45", "48", "51", "54", "57", "60", "63", "66", "69", "72", "75", "78", "81", "84", "87", "90", "93", "96", "99"]
mult_of_five = ["5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100"]
mult_of_both = ["15", "30", "45", "60", "75", "90"]
if number == mult_of_three
puts "Bit"
To find if an element is present in an Array, you can use include?. Here:
if mult_of_three.include?(number)
Example:
number = gets.chomp
36
# => "36"
mult_of_three.include?(number)
# => true
number = gets.chomp
98
# => "98"
mult_of_three.include?(number)
# => false

Grep and cut command in putty terminal

I'm trying to cut a specific line from several files.
I have about 300 json files.
file structure:
{
"FieldName1": "XXXX",
"FieldName2": "1",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "1",
.
.
.
}
,
{
"FieldName1": "XXXX",
"FieldName2": "2",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "2",
.
.
.
}
some of the files are written in one row:
{"FieldName1": "XXXX", "FieldName2": "3", "FieldName3": "XXX", "FieldName4": "XXX", "FieldName5": "3"}, {"FieldName1": "XXXX", "FieldName2": "4", "FieldName3": "XXX", "FieldName4": "XXX", "FieldName5": "4"}, ...
}
What i need to get in my output file is just a list of 2 fields from the files :
"FieldName1": "1",
"FieldName2": "1",
"FieldName1": "2",
"FieldName2": "2",
"FieldName1": "3",
"FieldName2": "3",
"FieldName1": "4",
"FieldName2": "4",
.
.
.
Is there an easy way to do so?
Thanks!
For the single line, you can use this gnu awk (do to multiple characters in RS)
awk -v RS='"FieldName' 'NR>1 {sub(/, "/,",");print RS$0}' file
"FieldName1": "XXXX",
"FieldName2": "3",
"FieldName3": "XXX",
"FieldName4": "XXX",
For multiple line use grep or awk like this:
awk '/FieldName/' file
"FieldName1": "XXXX",
"FieldName2": "1",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "1",

Resources