Skip to content

Commit

Permalink
Merge pull request #1873 from jqnatividad/1871-select-underscore-last…
Browse files Browse the repository at this point in the history
…-column

`select`: use underscore char (_) to indicate last column, replacing 9999 sentinel value
  • Loading branch information
jqnatividad authored Jun 12, 2024
2 parents 5d1d26e + 18e472e commit 1efa6b5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/cmd/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ selected using regular expressions.
Select the third column named 'Foo':
$ qsv select 'Foo[2]'
Select the first and last columns, 9999 is a special index for the last column:
$ qsv select 1,9999
Select the first and last columns, _ is a special character for the last column:
$ qsv select 1,_
Reverse the order of columns:
$ qsv select 9999-1
$ qsv select _-1
Sort the columns lexicographically. Note that you must provide a dummy selector:
$ qsv select 1 --sort
Expand Down
10 changes: 5 additions & 5 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ impl SelectorParser {
self.bump();
self.parse_quoted_name()?
} else {
if self.cur() == Some('_') {
self.bump();
return Ok(OneSelector::End);
}
self.parse_name()
};
Ok(if self.cur() == Some('[') {
Expand Down Expand Up @@ -318,14 +322,10 @@ impl OneSelector {
} else {
first_record.len() - 1
}),
OneSelector::Index(mut i) => {
OneSelector::Index(i) => {
if first_record.is_empty() {
return fail!("Input is empty.");
}
// 9999 is a sentinel value that means "last column".
if i == 9999 {
i = first_record.len();
}
if i < 1 || i > first_record.len() {
fail_format!(
"Selector index {i} is out of bounds. Index must be >= 1 and <= {}.",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ select_test!(

select_test!(
select_reverse_sentinel,
r#"9999-1"#,
r#"_-1"#,
"5-1",
["h1", "h4", "h[]3", "h2", "h1"],
["e", "d", "c", "b", "a"]
Expand Down

0 comments on commit 1efa6b5

Please sign in to comment.