为什么#字符在Perl中被视为“单词”字符?(Why is the # character seen as a 'word' character in Perl?)
为什么#字符在Perl中被视为“单词”字符? 或者我误解了这段代码应该如何工作?
#!/usr/bin/perl my $filename = "Something_with_#_sign.jpg"; $filename =~ s/ # substitute... [^ # characters which are NOT: \w # "word" characters ] # end of character classes /_/xg; # ...with an underscore print "$filename\n";
产量:
Something_with_#_sign.jpg
我会希望#号被替换为_(下划线)。
Why is the # character seen as a 'word' character in Perl? Or am I misunderstanding how this code is supposed to work?
#!/usr/bin/perl my $filename = "Something_with_#_sign.jpg"; $filename =~ s/ # substitute... [^ # characters which are NOT: \w # "word" characters ] # end of character classes /_/xg; # ...with an underscore print "$filename\n";
Yields:
Something_with_#_sign.jpg
I would have expected the # sign to have been replaced by an _ (underscore).
原文:https://stackoverflow.com/questions/32850681
更新时间:2021-10-30 09:10
最满意答案
你忘了填充成绩数组:)你需要:
grades[n] = grade;
在递增n之前; 否则grade数组将为空。 除此之外,n ++可以正常工作:
public static char[] grading(int[] testMarks) { char grade = '0'; char[] grades = new char[testMarks.length]; int value = 0; int n = 0; while (n < testMarks.length) { value = testMarks[n]; if (value >= 90) { grade = 'A'; } else if (value < 90 && value >= 75) { grade = 'B'; } else if (value < 75 && value >= 60) { grade = 'C'; } else if (value < 60 && value >= 50) { grade = 'D'; } else if (value < 50 && value >= 45) { grade = 'E'; } else { grade = 'F'; } grades[n] = grade; n++; } return grades; }
You forgot to populate the grades array :) you need to:
grades[n] = grade;
before incrementing n; otherwise the grades array would be empty. Other than that, n++ would work just fine:
public static char[] grading(int[] testMarks) { char grade = '0'; char[] grades = new char[testMarks.length]; int value = 0; int n = 0; while (n < testMarks.length) { value = testMarks[n]; if (value >= 90) { grade = 'A'; } else if (value < 90 && value >= 75) { grade = 'B'; } else if (value < 75 && value >= 60) { grade = 'C'; } else if (value < 60 && value >= 50) { grade = 'D'; } else if (value < 50 && value >= 45) { grade = 'E'; } else { grade = 'F'; } grades[n] = grade; n++; } return grades; }
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
问题是你没有签署扩展价值的标志 。 int x = 0; *((signed char *)&x) = (signed char)-1; printf("%08x\n", -1); // FFFFFFFF printf("%08x\n", 255); // 000000FF printf("%08x\n", x); // 000000FF 现场演示。 换句话说,将int的值设置为-1需要修改其所有字节。 但请注意,要这样做,您将依赖于两个实现定义的行为,使此代码不可移 ...
-
算法从java中的排列列表创建一个char数组(algorithm to create a char array from permutations list in java)[2022-12-09]
创建一个包含两个字段的类: private char letter; private int value; public(char letter){ this.letter = letter; value = 0; } //Setters and getters 然后在主要初始化数组时(通过for循环)将值设置为i + 1(除去0) for(int i = 0; i < .length; i ++){ //Assuming you initialize ... -
你的问题是"73167"不是一个数字。 它是由字符"7" , "3" , "1" , "6" , "7"的字符串。 Java中的char是Unicode代码点,ASCII / UTF-8中的'7'是55。 你想做 result *= Character.getNumericValue(numbers[i]); Doc for: Character.getNumericValue(char) Your problem is that "73167" isn't a number. It's a string ...
-
如果数组有int值,java - print()不输出char数组?(java - print() does not output char array if the array has int value?)[2022-02-05]
正如您所发现的,整数0的字符值和字符文字'0'是不同的。 要将数字转换为十进制数字的字符表示,您需要执行以下操作: int digit = ... // some value between 0 and 9 char ch = (char)(digit + '0'); 你也可以这样做: int digit = ... char ch = Character.forDigit(digit, radix); 其中radix的值在Character.MIN_RADIX和Character.MAX_RADIX ... -
自己实施携带应该是好的。 void inclementArray(char *id) { int i; for (i = 4; i >= 1; i--) { id[i]++; if (id[i] > '9') { id[i] = '0'; /* do carry and proceed to next digit */ } else { /* no carry, the c ...
-
int value = ((255 & 0xFF) << 24) | //alpha (((int)rgb[0] & 0xFF) << 16) | //red (((int)rgb[1] & 0xFF) << 8) | //green (((int)rgb[2] & 0xFF) << 0); //blue int value = ((255 & 0xFF) << 24) | //alpha (((int)rg ...
-
int数组不会获取char值(int array doesnt get char values)[2022-12-05]
你不应该通过%c得到整数既不是意图的整数变量的char值,而是你应该接近这样的东西 i = 0; do { printf("Enter a number: "); scanf("%d", &input[i]); i++; counter++; printf("Do you want to continue? (y/n) : "); scanf("%c", &inputs); } while(inputs == 'y'); 或者你可以预先获得整数输入的数量并循环以获得那么多的整数。 Yo ... -
Java char数组(Java char array)[2024-04-02]
代替 public static void count(char[] grade) { int numofa = 0, numofb = 0, numofc = 0, numofd = 0, numoff = 0; for (int i = 0; i < grade.length; i++){ if (grade[i] == 'A'){ numofa++; }else if (grade[i] == 'B'){ numofb++; ... -
通过在Java中测试int数组的值来创建char数组(Create a char array by testing the values of an int array in Java)[2023-02-09]
你忘了填充成绩数组:)你需要: grades[n] = grade; 在递增n之前; 否则grade数组将为空。 除此之外,n ++可以正常工作: public static char[] grading(int[] testMarks) { char grade = '0'; char[] grades = new char[testMarks.length]; int value = 0; int n = 0; while (n < testMarks ...