How to convert numbers in a string, into a hash

AshNaz87

Given a string, such as "ABCD 100", what is the most efficient way to convert it into {"ABCD": 100}, where "ABCD" is the key and 100 is an integer and the value?

Ilya

Assuming that you have more than one key-value pair, you can combine split, each_slice and map:

"ABCD 100 EFG 200".split
                  .each_slice(2)
                  .map {|k, v| [k.to_sym, v.to_i]}.to_h
#=> {:ABCD=>100, :EFG=>200}

Or with scan and groups:

"ABCD 100 EFG 200".scan(/([A-Z]+)\s([0-9]+)/)
                  .map! {|k, v| [k.to_sym, v.to_i]}.to_h
#=> {:ABCD=>100, :EFG=>200}

Use regex according to your actual issue, it is just an instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert a string of numbers into an array?

From Dev

How to convert a string of numbers to an array

From Dev

how to convert a string of numbers to a char

From Dev

How to convert query string in hash in perl

From Dev

How do I convert a string into hash in Ruby?

From Dev

How to convert query string in hash in perl

From Dev

how to convert the perl hash string reference to perl hash reference

From Dev

How can either convert a string into a hash or add it into a hash

From Dev

Kotlin : how to Convert String with HTML numbers to String without HTML numbers

From Dev

How to convert a string to numbers that include math operations in it?

From Dev

how to convert string values to numbers in a mixed array

From Dev

How to convert a string to numbers that include math operations in it?

From Dev

Convert String to Hash

From Dev

How do I convert an array list to hash separated string?

From Dev

How to convert a find_by_sql hstore string to a hash in Ruby on Rails

From Dev

In Ruby, how can I convert this string format to a hash

From Dev

How to convert this 'hash-like' string to a key value pair

From Java

How to convert numeric string to int in a RDD of string words and numbers?

From Dev

Convert the numbers of a set to String

From Dev

Convert/encode string to numbers

From Dev

Convert the numbers of a set to String

From Dev

Convert hash string to binary file

From Dev

ruby method to convert a hash to a string

From Dev

Convert string to tuple, hash, or array

From Dev

ruby method to convert a hash to a string

From Dev

T-SQL How to convert comma separated string of numbers to integer

From Dev

How do I convert a string of numbers to 24 time

From Dev

How can I convert a string of numbers to an array or vector of integers in Rust?

From Dev

How to convert a string of numbers into a list of int in python3

Related Related

  1. 1

    How to convert a string of numbers into an array?

  2. 2

    How to convert a string of numbers to an array

  3. 3

    how to convert a string of numbers to a char

  4. 4

    How to convert query string in hash in perl

  5. 5

    How do I convert a string into hash in Ruby?

  6. 6

    How to convert query string in hash in perl

  7. 7

    how to convert the perl hash string reference to perl hash reference

  8. 8

    How can either convert a string into a hash or add it into a hash

  9. 9

    Kotlin : how to Convert String with HTML numbers to String without HTML numbers

  10. 10

    How to convert a string to numbers that include math operations in it?

  11. 11

    how to convert string values to numbers in a mixed array

  12. 12

    How to convert a string to numbers that include math operations in it?

  13. 13

    Convert String to Hash

  14. 14

    How do I convert an array list to hash separated string?

  15. 15

    How to convert a find_by_sql hstore string to a hash in Ruby on Rails

  16. 16

    In Ruby, how can I convert this string format to a hash

  17. 17

    How to convert this 'hash-like' string to a key value pair

  18. 18

    How to convert numeric string to int in a RDD of string words and numbers?

  19. 19

    Convert the numbers of a set to String

  20. 20

    Convert/encode string to numbers

  21. 21

    Convert the numbers of a set to String

  22. 22

    Convert hash string to binary file

  23. 23

    ruby method to convert a hash to a string

  24. 24

    Convert string to tuple, hash, or array

  25. 25

    ruby method to convert a hash to a string

  26. 26

    T-SQL How to convert comma separated string of numbers to integer

  27. 27

    How do I convert a string of numbers to 24 time

  28. 28

    How can I convert a string of numbers to an array or vector of integers in Rust?

  29. 29

    How to convert a string of numbers into a list of int in python3

HotTag

Archive