logo
down
shadow

Using strings containing integers as table keys


Using strings containing integers as table keys

By : fabio
Date : November 22 2020, 10:48 AM
To fix this issue I realize this is usually not a great practice, but how would I use a string containing an integer (e.g. "7") as a table key? For example: , Like this:
code :
local myTable = {
  ["1"] = "Foo",
  ["2"] = "Bar"
}


Share : facebook icon twitter icon
What is the best way to store integers mapped to strings so that the keys can be ranges in python?

What is the best way to store integers mapped to strings so that the keys can be ranges in python?


By : Christophe Debove
Date : March 29 2020, 07:55 AM
this will help Given that you don't have any "gaps" in your keys, why you just don't store the beginning of each segment and then lookup with bisect like suggested?
code :
doodahs = (
    (0, "FOO"),
    (257, "BAR"),
    (258, "FISH"),
    (279, "MOOSE"),
    (280, "Darth Vader"),
    (65535, "Death to all newbies")
)
Python dictionary keys besides strings and integers?

Python dictionary keys besides strings and integers?


By : Gary Leaf
Date : March 29 2020, 07:55 AM
hop of those help? Let's go for something a bit more esoteric. Suppose you wanted to execute a list of functions and store the result of each. For each function that raised an exception, you want to record the exception, and you also want to keep a count of how many times each kind of exception is raised. Functions and exceptions can be used as dict keys, so this is easy:
code :
funclist = [foo, bar, baz, quux]

results    = {}
badfuncs   = {}
errorcount = {}

for f in funclist:
    try:
        results[f] = f()
    except Exception as e:
        badfuncs[f]   = e
        errorcount[type(e)] = errorcount[type(e)] + 1 if type(e) in errorcount else 1
Strings in Keys becoming Integers?

Strings in Keys becoming Integers?


By : Kate Anifer
Date : March 29 2020, 07:55 AM
Any of those help PHP converts numeric keys to integers at the time you create the array element. It isn't array_keys that is doing it. But there is a hack to get string numeric keys:
code :
$a = new stdClass;
$a->{"0"} = "zero";
$a = (array) $a;
var_dump($a);
array(1) {
  ["0"]=>
  string(4) "zero"
}
$MNTH["001"] = "January";
$MNTH["012"] = "December";
Pyyaml - Using different styles for keys and integers and strings

Pyyaml - Using different styles for keys and integers and strings


By : user3629824
Date : March 29 2020, 07:55 AM
I wish this help you You can at least preserve the original flow/block style for the various elements with the normal yaml.dump() for some value of "normal".
What you need is a loader that saves the flow/bcock style information while reading the data, subclass the normal types that have the style (mappings/dicts resp. sequences/lists) so that they behave like the python constructs normally returned by the loader, but have the style information attached. Then on the way out using yaml.dump you provide a custom dumper that takes this style information into account.
code :
import ruamel.yaml as yaml

infile = yaml.load(open('yamlfile'), Loader=yaml.RoundTripLoader)

for key, value in infile['main'].items():
    if key == 'keepalivetimeout':
        item = value['item']
        item['keepalivetimeout'] = 400

print yaml.dump(infile, Dumper=yaml.RoundTripDumper)
main:
  directory:
    options:
      directive: options
      item:
        options: Stuff OtherStuff MoreStuff
  directoryindex:
    item:
      directoryindex: stuff.htm otherstuff.htm morestuff.html
  fileetag:
    item:
      fileetag: Stuff
  keepalive:
    item:
      keepalive: Stuff
  keepalivetimeout:
    item:
      keepalivetimeout: 400
import ruamel.yaml as yaml

# the scalar emitter from emitter.py
def process_scalar(self):
    if self.analysis is None:
        self.analysis = self.analyze_scalar(self.event.value)
    if self.style is None:
        self.style = self.choose_scalar_style()
    split = (not self.simple_key_context)
    # VVVVVVVVVVVVVVVVVVVV added
    try:
        x = int(self.event.value)  # might need to expand this
    except:
        # we have string
        if split:
            self.style = "'"
        else:
            self.style = '"'
    # ^^^^^^^^^^^^^^^^^^^^
    # if self.analysis.multiline and split    \
    #         and (not self.style or self.style in '\'\"'):
    #     self.write_indent()
    if self.style == '"':
        self.write_double_quoted(self.analysis.scalar, split)
    elif self.style == '\'':
        self.write_single_quoted(self.analysis.scalar, split)
    elif self.style == '>':
        self.write_folded(self.analysis.scalar)
    elif self.style == '|':
        self.write_literal(self.analysis.scalar)
    else:
        self.write_plain(self.analysis.scalar, split)
    self.analysis = None
    self.style = None
    if self.event.comment:
        self.write_post_comment(self.event)


infile = yaml.load(open('yamlfile'), Loader=yaml.RoundTripLoader)

for key, value in infile['main'].items():
    if key == 'keepalivetimeout':
        item = value['item']
        item['keepalivetimeout'] = 400

dd = yaml.RoundTripDumper
dd.process_scalar = process_scalar

print '---'
print yaml.dump(infile, Dumper=dd)
---
"main":
  "directory":
    "options":
      "directive": 'options'
      "item":
        "options": 'Stuff OtherStuff MoreStuff'
  "directoryindex":
    "item":
      "directoryindex": 'stuff.htm otherstuff.htm morestuff.html'
  "fileetag":
    "item":
      "fileetag": 'Stuff'
  "keepalive":
    "item":
      "keepalive": 'Stuff'
  "keepalivetimeout":
    "item":
      "keepalivetimeout": 400
Want to create a NSMutableDictionary with the keys as integers and objects as strings in Objective-C

Want to create a NSMutableDictionary with the keys as integers and objects as strings in Objective-C


By : John Rodriguez
Date : March 29 2020, 07:55 AM
it should still fix some issue I want to declare and initialize a NSMutableDictionary with a int:NSString combination.
code :
NSMutableDictionary *gameDictionary = [@{@1 : @"SquareOne", @2 : @"SquareTwo", @3 : @"SquareThree", @4 : @"SquareFour", @5 : @"SquareFive", @6 : @"SquareSix", @7 : @"SquareSeven", @8 : @"SquareEight", @9 : @"SquareNine"} mutableCopy];
gameDictionary[@1] = nil;
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org