[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Question that baffles AI (all of them)
From: |
Greg Wooledge |
Subject: |
Re: Question that baffles AI (all of them) |
Date: |
Sat, 15 Jun 2024 17:57:39 -0400 |
On Sat, Jun 15, 2024 at 05:30:17PM -0400, Saint Michael wrote:
> in this code:
> data="'1,2,3,4','5,6,7,8'"
> how can I get my (a) and (b) arguments right?
> The length of both strings is unpredictable.
> a="1,2,3,4" and b="5,6,7,8""
This is a parsing problem. Bash is not a particularly good choice for
writing a custom parser like this. It'll be slow as hell.
It *looks* like your "data" comes from a CSV file, or some variant of
a CSV file. If this is the case, then you've brought us an X-Y problem.
There are CSV parsing libraries in several different languages.
You should consider switching to one of those languages, and writing
your program with appropriate tools.
For example, here's a version using Tcllib's csv package. It assumes the
quote character is " so we have to change it to ' in the call to split:
hobbit:~$ cat foo
#!/usr/bin/tclsh8.6
package require csv
set data {'1,2,3,4','5,6,7'}
set list [csv::split $data , ']
puts "list item 0 is <[lindex $list 0]>"
puts "list item 1 is <[lindex $list 1]>"
hobbit:~$ ./foo
list item 0 is <1,2,3,4>
list item 1 is <5,6,7>
Similar packages probably exist in all of the major scripting languages
that are not shells.
I've Cc'ed help-bash for this; that's where the question belongs.