How can I replace 404 to 403 only in the 2nd if block, my cursor is now at row 45.
41 if( error.response.status === 404 ) {
42 router.replace('/404')
43 }
44
45 if( error.response.status === 404 ) {
46 router.replace('/404')
47 }
If your cursor is at line 45, you can do this, ,+2 means including current line and next 2 lines
:,+2s/404/403/g
If your cursor is at any position, you can do this
:45,+2s/404/403/g
Related
I'm currently taking a class where we are learning Rust. I am running into a really weird issue and I'm not sure what is causing it.
So I have two vectors, and I'm looping through them to print their contents.
when I do the following, it prints out fine,
for index in 0..=delimited_record.capacity()-1{
let cell = delimited_record.get(index).unwrap();
println!("{}",cell);
}
the result is as follows
99
87.4
76
mike
73
95.5
100
gary
however, the following small change makes the 0th index of the second vector come out blank
for index in 0..=delimited_record.capacity()-1{
let cell = delimited_record.get(index).unwrap();
print!("{}",cell);
println!(" - {}",index);
}
the result is as follows
99 - 0
87.4 - 1
76 - 2
mike - 3
- 0
95.5 - 1
100 - 2
gary - 3
The thing is, if I try to do anything besides do a regular print, it'll come back blank. These values are all strings, so what I'm trying to do is convert these values into floats (not the names, just the numbers), but it keeps crashing whenever I try to parse the second array at the 0th index, and it seems it's because its blank for some reason.
Does anyone know what is causing this?
Edit
Here is the code in its entirety, Theres a lot of stuff commented out since i was trying to find out was was causing the issue
use std::{fs, ptr::null};
fn main() {
println!("Hello, world!");
println!("TEST");
let sg = StudentGrades{
name: String::from("mike"),
grades: vec![100.0,80.0,55.0,33.9,77.0]
//grades: vec!['A','B','C','A']
};
let mut cg = CourseGrades{
student_records: Vec::new()
};
cg.from_file("texttest.txt".to_owned());
}
struct StudentGrades{
name: String,
grades: Vec<f32>
}
impl StudentGrades{
fn average(&self)-> f32 {
let mut sum = 0.0;
for grade in self.grades.iter(){
sum += grade;
}
return sum / self.grades.capacity() as f32;
}
fn grade(&self) -> char {
let score = self.average();
let scure_truncated = score as i64 / 1;
match scure_truncated{
0..=59=> return 'F',
60..=69=> return 'D',
70..=79=> return 'C',
80..=89=> return 'B',
_=> return 'A',
}
}
}
struct CourseGrades{
student_records: Vec<StudentGrades>
}
impl CourseGrades{
fn from_file(&mut self, file_path:String){
let mut contents = fs::read_to_string(file_path)
.expect("Should have been able to read the file");
contents = contents.replace(" ","");
let student_rows: Vec<&str> = contents.rsplit('\n').collect();
for student_record in student_rows.iter(){
let mut student_grades:Vec<f32> = Vec::new();
let delimited_record: Vec<&str> = student_record.rsplit(",").collect();
//println!("{}",delimited_record.get(0).unwrap());
//delimited_record.iter().for_each(|x| println!("{}",x));
for index in 0..=delimited_record.len()-1{
//println!("{}",delimited_record.get(index).unwrap().parse::<f32>().unwrap_or_default());
//println!("{}",delimited_record.get(0).unwrap().parse::<i32>().unwrap_or_default());
//student_grades.push(delimited_record.get(index).unwrap().parse::<f32>().unwrap());
let cell = delimited_record.get(index).unwrap();
print!("{}",cell);
println!(" - {}",index);
//println!(" - {}", index < delimited_record.capacity()-1);
if index < delimited_record.len(){
//let grade = cell.parse::<f32>().unwrap();
//student_grades.push(cell.parse::<f32>().unwrap());
//student_grades.push(delimited_record.get(index).unwrap().parse::<f32>().unwrap());
}
else{
/*self.student_records.push(StudentGrades{
name:cell.parse::<String>().unwrap(),
grades:student_grades.to_owned()
});*/
}
}
}
}
}
the testtext.txt file is in the root of the project folder and is just a text file with the following contents
gary, 100, 95.5, 73
mike, 76, 87.4, 99
if I embed it directly, it works just fine, which makes me think there may be something weird when it reads the file
Your file has CR LF (\r\n, Windows-style) line endings, but you’re only splitting on \n. Your grade ends up ending with a CR. This is invisible when you println! it on its own (since the result is CR LF), but if you print something after it on the same line, the CR has returned the cursor to the start of the line and the second thing being printed will write over it.
7 73 73 73 73
^ ^ ^ ^ 9
^ ^
----------------------------------------
'7' '3' '\r' '\n' '9' …
7 73 73 3 -
^ ^ ^ ^ ^ ^
----------------------------------------
'7' '3' '\r' ' ' '-' …
One way to fix this is to strip all whitespace from every cell, including CR:
let cell = delimited_record.get(index).unwrap().trim();
And for debugging, consider formatting with "{:?}" instead of "{}", which will show a literal with invisible characters escaped instead of writing out whatever the string contains directly.
I am trying to convert length message to ascii.
My length message is like
var ll = "0170";
In node js , is there some kind of function which converts into ascii?
Please help?
Here's a simple function(ES6) which converts a string into ASCII characters using charCodeAt()
const toAscii = (string) => string.split('').map(char=>char.charCodeAt(0)).join(" ")
console.log(toAscii("Hello, World"))
Output:
-> 72 101 108 108 111 44 32 87 111 114 108 100
You could create a prototype function aswell. There are many solutions :)
you can't have an ascii code for a whole string.
An ascii code is an integer value for a character, not a string. Then for your string "0170" you will get 4 ascii codes
you can display these ascii codes like this
var str = "0170";
for (var i = 0, len = str.length; i < len; i++) {
console.log(str[i].charCodeAt());
}
Ouput : 48 49 55 48
use charCodeAt() function to covert asscii format.
var ll = "0170";
function ascii (a) { return a.charCodeAt(); }
console.log(ascii(ll[0]),ascii(ll[1]), ascii(ll[2]), ascii(ll[3]) )
result:
48 49 55 48
I have following text file:
079082084072079032084069067072000000000,0
082078032049050032067072065082071069000,1
076065066032065083083084000000000000000,0
082078032049050072082000000000000000000,1
082078032049050072082000000000000000000,1
082078032049050072082000000000000000000,1
070083087032073073032080068000000000000,0
080067065032049050032072082000000000000,0
082078032056072082000000000000000000000,1
070083087032073073073000000000000000000,0
082078032087069069075069078068000000000,1
082078032049050072082000000000000000000,1
077065073078084032077069067072032073073,0
082078032049050072082000000000000000000,1
080067065032049050032072082000000000000,0
082078032049050072082000000000000000000,1
I need too matrices:
X size 16x13
Y size 16x1
I want to separate each row of the file into 13 values, example:
079 082 084 072 079 032 084 069 067 072 000 000 000
Is it possible to import it into octave using textread function?
If no, can it be done using Linux bash command?
Yes, you can do this with textscan (see bottom if you really want to use textread:
octave> txt = "079082084072079032084069067072000000000,0\n082078032049050032067072065082071069000,1";
octave> textscan (txt, repmat ("%3d", 1, 13))
ans =
{
[1,1] =
79
82
[1,2] =
82
78
[1,3] =
84
32
[1,4] =
72
49
[...]
Note that you are reading them as numeric values, so you do not get the preceding zeros. If you want them, you can either read them as string by using "%3s" in the format (extra trouble to handle and reduced performance since you will then be handling cell arrays).
Since you are reading from a file:
[fid, msg] = fopen ("data.txt", "r");
if (fid)
error ("failed to fopen 'data.txt': %s", msg);
endif
data = textscan (fid, repmat ("%3d", 1, 13));
fclose (fid);
If you really want to use textread:
octave> [d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13] = textread ("data.txt", repmat ("%3d", 1, 13))
d1 =
79
82
76
[...]
d2 =
82
78
65
[...]
or:
octave> data = cell (1, 13);
octave> [data{:}] = textread ("data.txt", repmat ("%3d", 1, 13))
data =
{
[1,1] =
79
82
76
[...]
[1,2] =
82
78
65
[...]
If you need to capture the value after the comma (not really part of your original question), you can use:
octave> textscan (txt, [repmat("%3d", 1, 13) ",%1d"])
ans =
{
[1,1] =
79
82
[1,2] =
82
78
[1,3] =
84
32
[...]
[1,14] =
0
1
}
You can do this pretty easily by reading three characters at a time using read in the shell:
while IFS="${IFS}," read -rn3 val tail; do
[[ $tail ]] && echo || printf '%s ' "$val"
done < file
This implementation assumes that if we encounter a value after the comma, we should go to the next line.
I have created the following code through making a while loop. I cannot tell why the loop is set to look at 17 lines and not 15 lines.
while (scan.hasNext())
{
selectedUserName.setText(scan.next());
if(scan.nextLine().toString() == "Male")
{
male = new JRadioButton("Male", true);
}
else if(scan.nextLine().toString() == "Female")
{
female = new JRadioButton("Female", true);
}
selectedGenderText.setText(scan.nextLine());
if(scan.next().toString() == "Warrior")
{
userClassBox.setSelectedItem(classes[0]);
}
else if(scan.next().toString() == "Barbarian")
{
userClassBox.setSelectedItem(classes[1]);
}
else if(scan.next().toString() == "Mage")
{
userClassBox.setSelectedItem(classes[2]);
}
else if(scan.next().toString() == "Thief")
{
userClassBox.setSelectedItem(classes[3]);
}
selectedClassText.setText(scan.nextLine());
pointsText.setText(scan.nextLine());
pointsSlider.setValue(Integer.parseInt(scan.nextLine()));
intelligenceText.setText(scan.nextLine());
intelligenceSlider.setValue(Integer.parseInt(scan.nextLine()));
dexterityText.setText(scan.nextLine());
dexteritySlider.setValue(Integer.parseInt(scan.nextLine()));
strengthText.setText(scan.nextLine());
strengthSlider.setValue(Integer.parseInt(scan.nextLine()));
wisdomText.setText(scan.nextLine());
wisdomSlider.setValue(Integer.parseInt(scan.nextLine()));
}
My input file has 15 lines to match the code above,
Warrior
Male
Male
Warrior
Warrior
0
0
10
10
30
30
60
60
0
0
Do the if/else statements reference two new lines? When I run the program it gets stuck on the following line-
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at CharacterCreation$OpenCharListener.actionPerformed(CharacterCreation.java:450)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
This reference the last two lines starting at -
wisdomText.setText(scan.nextLine());
wisdomSlider.setValue(Integer.parseInt(scan.nextLine()));
Why does the .nextLine() functionality look at 17 lines instead of 15?
Note that Scanner.nextLine() consumes data from the scanner. So in this example:
if(scan.nextLine().toString() == "Male")
{
male = new JRadioButton("Male", true);
}
else if(scan.nextLine().toString() == "Female")
{
female = new JRadioButton("Female", true);
}
on the first if statement, you'll consume data from the scanner, and if the conditional is false, you'll then consume whatever follows it within the else-if statement.
You probably just want to do String input = scan.nextLine(); and then compare against that stored string.
I am drawing an signature like this as given below and taking X Y cordinate and saving it to the arry list.
Bitmap bmp;
//Graphics object
Graphics graphics;
//Pen object
Pen pen = new Pen(Color.Black);
// Array List of line segments
ArrayList pVector = new ArrayList();
//Point object
Point lastPoint = new Point(0, 0);
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
// process if currently drawing signature
if (!drawSign)
{
// start collecting points
drawSign = true;
// use current mouse click as the first point
lastPoint.X = e.X;
lastPoint.Y = e.Y;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// process if drawing signature
if (drawSign)
{
if (graphics != null)
{
// draw the new segment on the memory bitmap
graphics.DrawLine(pen, lastPoint.X, lastPoint.Y, e.X, e.Y);
pVector.Add(lastPoint.X + " " + lastPoint.Y + " " + e.X + " " + e.Y);
// update the current position
lastPoint.X = e.X;
lastPoint.Y = e.Y;
// display the updated bitmap
Invalidate();
}
}
}
Using the arrylist (pVector) I am saving the values to the database as string(singature ) and aslo as image as given below
//Saving value to Database
ArrayList arrSign = new ArrayList();
arrSign = this.signatureControl.getPVector();
string singature = "";
for (int i = 0; i < arrSign.Count; i++)
{
singature = singature + arrSign[i].ToString() + "*";
}
the string singature wiil be like this
60 46 59 48*59 48 59 51*59 51 59 53*59 53 60 49*60 49 61 44*61 44 62 38*62 38 64 31*64 31 67 23*67 23 70 14*70 14 72 10*72 10 75 3*75 3 77 -2*77 -2 76 2*76 2 75 6*75 6 72 17*72 17 71 24*71 24 69 31*69 31 68 46*68 46 67 59*67 59 68 71*68 71 69 79*69 79 70 86*70 86 71 89*71 89 71 93*71 93 71 95*71 95 71 97*71 97 70 95*70 95 69 88*69 88 68 81*68 81 69 77*69 77 69 68*69 68 71 60
//Saving as Image file
Pen pen = new Pen(Color.Black);
string[] arrStr = (signature.Split('*'));
Graphics graphics;
Bitmap bmp = new Bitmap(300, 200);
graphics = Graphics.FromImage(bmp);
graphics.Clear(Color.White);
for (int i = 0; i < arrStr.Length - 2; i++)
{
string[] strArr = new string[4];
strArr = ((arrStr[i].ToString()).Split(' '));
graphics.DrawLine(pen, Convert.ToInt32(strArr[0].ToString()), Convert.ToInt32(strArr[1].ToString()),
Convert.ToInt32(strArr[2].ToString()), Convert.ToInt32(strArr[3].ToString()));
}
string pathToCopyImage = systemBus.TempFile;
bmp.Save(pathToCopyImage + "\\" + dsReportDetails.Tables["tblDelivery"].Rows[0]["PKDelivery"].ToString() + "_Signature.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
bmp.Dispose();
My problem is that after Saving the signature as Image file I am not able to convert it back to arrylist like the one that i am used to save the value in the database.
ie I need to convert the image file back to as given below format
60 46 59 48*59 48 59 51*59 51 59 53*59 53 60 49*60 49 61 44*61 44 62 38*62 38 64 31*64 31 67 23*67 23 70 14*70 14 72 10*72 10 75 3*75 3 77 -2*77 -2 76 2*76 2 75 6*75 6 72 17*72 17 71 24*71 24 69 31*69 31 68 46*68 46 67 59*67 59 68 71*68 71 69 79*69 79 70 86*70 86 71 89*71 89 71 93*71 93 71 95*71 95 71 97*71 97 70 95*70 95 69 88*69 88 68 81*68 81 69 77*69 77 69 68*69 68 71 60
Will any one help me please
It is not very easy to get your "signature string" back from image so you can just add you "string signature" to saved image as a metadata tag of image (as a Description for example). So then you read your image back you don't need to recognize "signature string" from image, you can just read it from metadata as a string. Msdn has a nice article about image metadata and api to work with them. http://msdn.microsoft.com/en-us/library/ms748873.aspx
By the way, your code for concatenating "signature string" is slow and memory consuming. It is better to use StringBuilder in such situations in .Net. And overall strings are not the best data structure to store list of points. But it depends on requirements for your app.