gavinhoward / bc

An implementation of the POSIX bc calculator with GNU extensions and dc, moved away from GitHub. Finished, but well-maintained.
https://git.gavinhoward.com/gavin/bc
Other
145 stars 29 forks source link

Array - Multiple values #70

Closed STSMHQ closed 8 months ago

STSMHQ commented 8 months ago

Hi, @gavinhoward,

I read the documentation file and it seems that arrays can only old a single value per each index (a number or a string). Is there any workaround to be able to store more than one single value in an array?

# This works
bc_example[0] = 1;

# This doesn't
bc_example[1] = [1, 2, 3];
bc_example[2] = [1, "Random", 3, "Another"];

print bc_example[2][1] # Random

As I already stated before in another issue, I'm using your amazing tool to keep track of my expenses and it would be amazing to be able to create a "matrix" or an "array of arrays" as people often call it. Although I know that my use case isn't the general one and that the type of feature that I'm requesting doesn't make much sense considering the main goal/purpose of this tool, I'm opening this issue to know if what I want is somehow possible using any syntax workaround not documented in the above file.

gavinhoward commented 8 months ago

Unfortunately, there is not any workaround, and that is on purpose. The bc language is just too restricted to go any further without serious work.

It is possible, yes. However, I believe that would be a mistake.

I once said this:

Good software design includes putting whatever complexity must exist where it best fits.

-- "Justifying a Backwards Design Decision for Yao

So one of the skills of a good programmer is knowing where complexity should go.

If you need multiple values in one array slot, it sounds like you need structs, objects, or some other compound data thing. That sort of complexity should live in a "proper" programming language.

So I suggest that you use a "proper" programming language with arbitrary-precision numbers, like Python.

That said, I don't want to just hang you out to dry with no solution!

You're using my bc because it was the best for the job until now, right? Can you tell me why that is?

You've mentioned that you use it for finances; unlike most other math, finances basically require base 10 math, so is that why my bc was best?

If so, I might have a solution for you.

Do you know Python?

If so, I could learn how to create a C extension for Python, and then I could use my bc's library form to create a Python extension to use my bc's library. That way, you could use Python, but still have the decimal-based math you need for finances, and I wouldn't have to do a ton of work to add multi-values to bc while also adding my bc to Python.

As an extra bonus, if you use Python, you could then have your files be output to JSON or another well-known format. And hey, I may even be able to help you convert your scripts to Python if you send them to me. (Do it through email, not this bug report!)

Will that work for you?

STSMHQ commented 8 months ago

Hi, @gavinhoward,

I'm using your bc tool for over a year to keep track of my expenses and everything related to my personal finances. The main reason behind this decision is what you just said, generally speaking, finances use base 10 math and bc-gh allows me to have arbitrary-precision numbers on it. The second big reason is you. The passion you put into your projects is truly amazing. I know that as long as you're alive and well, you'll keep this project running and that's a important thing for me. Also, your support as shown here in this issue is very, very good. To end, I'm a follower of your blog since the beginning of 2022.

I don't know Python. I could learn it but I don't like much its' syntax and the biggest part of its' design choices. I'll probably take this chance to improve my NuShell knowledge and try to integrate bc-gh into it. With NuShell, I know that I can work with well-known formats like JSON/YAML/TOML in order to be able to implement my desired workflow. Thank you for everything and keep up the excellent work.

gavinhoward commented 8 months ago

You flatter me! :) But more seriously, thank you for the compliments; I struggle to see worth in my code, and that helps a lot.

I'm sorry I couldn't help more. But feel free to ask any questions to help with your port to NuShell. I'm happy to help with that since I was not much help here!

STSMHQ commented 8 months ago

You flatter me! :) But more seriously, thank you for the compliments; I struggle to see worth in my code, and that helps a lot.

Your code and your blog posts truly make a difference to my life 💙

I'm sorry I couldn't help more. But feel free to ask any questions to help with your port to NuShell. I'm happy to help with that since I was not much help here!

You don't have to be sorry. I read the documentation of NuShell and I think I'm able to implement my current workflow with bc-gh on it pretty easily. Thank you for all your spent time and help offered. Have a nice rest of week!

gavinhoward commented 8 months ago

You're welcome.

Hey, when you finish your NuShell implementation, I would love it if you could email it to me; I'm almost done with my new language, and I'd love to use it as a test application.

STSMHQ commented 8 months ago

I'm still studying it. As NuShell didn't reach yet the first ever stable level, I don't know if I should implement all of my workflow directly using it. Currently, I'm just exploring the data processing pipeline features that they have against a workflow using sed/awk and your bc-gh, for instance. All I'm doing is pretty much pick a JSON file, iterate over it, apply some editions/additions using your bc-gh and then save it again to a JSON file. Hope you like NuShell as much as I do and one day in the future, it can have the same popularity as zsh.

If in the future I implement something that I'm proud of, I'll happily share it with you, Gavin.