Create byte ranges for to match keys starting with a particular letter - scala

I'm doing HBase range scans, and I need to return all rows that begin with a particular letter. Given a single letter as input, how do I create the correct startRow and endRow keys?

val letter = "s"
val startRow = letter
val endRow = (letter.charAt(0) + 1).toChar.toString

Related

How to calculate number of occurrence of a character at beginning in a List of String using Scala

I am new to Scala and I want to calculate number of occurrences of a character in which start with a particular alphabet in a list of Strings.
For example-
val test1 : List[String] = List("zero","zebra","zenith","tiger","mosquito")
I have defined above List of Strings and I want to calculate count of all strings which start with "z".
I tried with below code-
scala> test2.count(s=> s.charAt(0) == "z")
res7: Int = 0
It is giving me result as 0. I am not sure what I am doing wrong. Please suggest.
Character values are delimited by single quotes. Double quotes are reserved for strings:
val test : List[String] = List("zero","zebra","zenith","tiger","mosquito")
test.count(_.charAt(0) == 'z') // 3: Int
you can simply use filter and find the length of the list
println(test1.filter(_.startsWith("z")).length)
If you want to ignore the cases (uppercase or lowercase) you can add .toLowerCase as
println(test1.filter(_.toLowerCase.startsWith("z")).length)
I hope the answer is helpful

Python cut words in list after certain length

I have a List of entries in the csv file records. I want to limit the length of the elements to 50 characters and save it into the list. My approach does not work.
def readfile():
records = []
with open(fpath, 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter='|')
for row in csvreader:
if len(row) == 0:
continue
records.append([row[1]] + [x.strip() for x in row[3]])
return records
def cut_words(records):
for lines in records:
for word in lines:
word = word[0:50]
return records
it does not seem to be saved in the list.... thanks

Capitalize the first and last letter of three letter words in a string

I am trying to capitalize the first and last letter of only the three letter words in a string. So far, I have tried
spaces = strfind(str, ' ');
spaces = [0 spaces];
lw = diff(spaces);
lw3 = find(lw ==4);
a3 = lw-1;
b3 = spaces(a3+1);
b4 = b3 + 2 ;
str(b3) = upper(str(b3));
str(b4) = upper(str(b4);
we had to find where the 3 letter words were first so that is what the first 4 lines of code are and then the others are trying to get it so that it will find where the first and last letters are and then capitalize them?
I would use regular expressions to identity the 3-letter words and then use regexprep combined with an anonymous function to perform the case-conversion.
str = 'abcd efg hijk lmn';
% Custom function to capitalize the first and last letter of a word
f = #(x)[upper(x(1)), x(2:end-1), upper(x(end))];
% This will match 3-letter words and apply function f to them
out = regexprep(str, '\<\w{3}\>', '${f($0)}')
% abcd EfG hijk LmN
Regular expressions are definitely the way to go. I am going to suggest a slightly different route, and that is to return the indices using the tokenExtents flag for regexpi:
str = 'abcd efg hijk lmn';
% Tokenize the words and return the first and last index of each
idx = regexpi(str, '(\<w{3}\>)', 'tokenExtents');
% Convert those indices to upper case
str([idx{:}]) = upper(str([idx{:}]));
Using the matlab ipusum function from the File Exchange, I generated a 1000 paragraph random text string with mean word length 4 +/- 2.
str = lower(matlab_ipsum('WordLength', 4, 'Paragraphs', 1000));
The result was a 177,575 character string with 5,531 3-letter words. I used timeit to check the execution time of using regexprep and regexpi with tokenExtents. Using regexpi is an order of magnitude faster:
regexpi = 0.013979s
regexprep = 0.14401s

mapping columns using their headers in macro

I have two worksheets. I have to copy values to first worksheet from second worksheet but on basis of column name.
For coping data from column G to C I am using-
If Wks2.Range("C" & I) <> Wks.Range("G" & J).Value Then
Wks2.Range("C" & I).Value = Wks.Range("G" & J)
End If
But the problem here is that column sequence keeps on changing in secong one. So mapping cannot be hardcoded on column alphabet.
I not sure how to map them using column headers.
Thank you in advance.
You can search ColumnName in Header. After find the matching item ,retrieve its row as reference ,
Dim cellRef as integer
Set e = Worksheets(2).Rows(1).find(ColumnName)
If not e is nothing then
cellRef = e. column
End if
OK.
So , assume that your columnnames are in Row#1 of worksheets(2) = wks2
Yoh have to find the letter of columnName ( here is "C" )
SearchColumn : wanted columnname
Dim e As Object
Set e = Nothing
Set e = wks2.Range("1:1").Find(SearchColumn)
If not e is nothing then
Dim colAddress As String
colAddress = e.Address
Dim colLetter As String
colLetter = Mid(colAddress, 2, InStr(2, colAddress, "$", vbTextCompare) - 2)
If Wks2.Range(colLetter & I) <> Wks.Range("G" & J).Value Then
Wks2.Range(colLetter & I).Value = Wks.Range("G" & J)
End
Else
msgbox("Can not find " & SearchColumn)
End if
End If
First of All , we find the column ( e )
e.address returns the found column address
then colLetter store the column letter of it.
Please inform me for any question
C

matlab: reading numbers that also contain characters

I have to read a textfile which contains a list of companycodes. The format of the textfile is:
[1233A12; 1233B88; 2342Q85; 2266738]
Even if I have read the file? Is it possible to compare these numbers with regular numbers? Because I have the codes from two different data-bases and one of them has regular firmnumbers (no characters) and the other has characters inside the firmnumbers.
Btw the file is big (50+mb).
Edit: I have added an additional number in the example because not all the numbers have a character inside
If you want to compare part of a string with a number, you could do it as follows:
combiString = '1234AB56'
myNumber= 1234
str2num(combiString(1:4))==myNumber
str2num(combiString(7:8))==myNumber
You can achieve this result by using regular expressions. For example, if str = '1233A12' you can write
nums = regexp(str, '(\d+)[A-Z]*(\d+)', 'tokens');
str1 = nums{1}(1);
num1 = str2num(str1{1});
str2 = nums{1}(2);
num2 = str2num(str2{1});