Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string repeat does not process beyond the first argument #5988

Closed
ssjhv opened this issue Jul 12, 2019 · 6 comments
Closed

string repeat does not process beyond the first argument #5988

ssjhv opened this issue Jul 12, 2019 · 6 comments

Comments

@ssjhv
Copy link

ssjhv commented Jul 12, 2019

Fish version 3.0.2
OS: Debian Linux

string commands handle multiple arguments, e.g., "string length a bb ccc dddd" return four elements 1 2 3 4.

It looks like string repeat is an exception here.

~> string repeat -n 2 a bb ccc dddd
aa

It would be better if string repeat handles multiple arguments consistently with the other string commands.

(My motivation here is to use "string repeat -n 1 $var" as a way to return array $var. Currently one can either manually loop and echo, or use "string join \n $var", but loops are too long, and string join feels like it returns a single element, and automatic unpacking via newline split feels weird. I feel string repeat could be a good alternative to these.)

@faho
Copy link
Member

faho commented Jul 15, 2019

It would be better if string repeat handles multiple arguments consistently with the other string commands.

What is "consistently" here? Would it repeat each element n times, for "a a bb bb ccc ccc dddd dddd", or go through the list n times, for "a bb ccc dddd a bb ccc dddd"? Or something else?

My motivation here is to use "string repeat -n 1 $var" as a way to return array $var

What do you mean by this? You mean "print the variable, one line per element"? That's typically done via printf %s\n $var. I'd think repeat would be the absolute last thing I'd reach for.

@faho faho added this to the fish 3.1.0 milestone Jul 15, 2019
@ssjhv
Copy link
Author

ssjhv commented Jul 19, 2019

If I understand correct, all (other than repeat) string commands handle multiple arguments equivalent to
for arg in $argv; string ... $arg; end

So "consistent" repeat should return "aa bbbb cccccc dddd".

@ssjhv
Copy link
Author

ssjhv commented Jul 19, 2019

Oops. "aa bbbb cccccc dddddddd".

@ssjhv
Copy link
Author

ssjhv commented Jul 19, 2019

By the way, I tried "printf %s\n $var" to print variable, one line per element. And in my opinion this is not a good solution, because it prints one empty line when [ (count $var) -eq 0 ].

~> set -l temp
~> count $temp
0
~> count (printf %s\n $temp)
1

@ridiculousfish
Copy link
Member

You might like set --show temp for understanding a variable's value.

@ridiculousfish
Copy link
Member

In 7a0bddf we repeat each argument in turn.

@zanchey zanchey removed the RFC label Jan 12, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants