我试图计算textarea的每一行中的字符。
我试图调整此代码来计算音节std::find_if
但没有成功。任何帮助将不胜感激。
function $CharCount($input) {
$("[name=set_" + $input + "]").keyup(function() {
var arrayOfLines = $("[name=set_" + $input + "]").val().match(/[^\r\n]+/g);
var tempArr = [];
var $content;
var char;
var $result;
for (var i = 0; i < arrayOfLines.length; i++) {
$content = arrayOfLines[i];
$result = $content.val().length;
tempArr.push($result);
}
$("[name=set_" + $input + "_content]").val(tempArr);
});
}
(function($) {
$CharCount("a");
})(jQuery);
&#13;
<textarea rows="8" cols="3" class="alignright" name="set_a_syllable_count" readonly="readonly" /></textarea>
<textarea rows="8" cols="30" name="set_a"></textarea>
&#13;
答案 0 :(得分:2)
[06 Sep 2016 23:13:58] [jmxtrans-result-6] 21573814 WARN (com.googlecode.jmxtrans.jmx.ResultProcessor$1:60) - Could not write results [Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=Metaspace, values={init=0, committed=20185088, max=-1, used=19304024}, epoch=1473183838891, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=PS Old Gen, values={init=87031808, committed=87031808, max=1375731712, used=16885688}, epoch=1473183838901, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=PS Eden Space, values={init=32505856, committed=24117248, max=686817280, used=4032656}, epoch=1473183838903, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=Compressed Class Space, values={init=0, committed=2359296, max=1073741824, used=2134656}, epoch=1473183838904, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=Code Cache, values={init=2555904, committed=9633792, max=251658240, used=9103552}, epoch=1473183838905, keyAlias=memorypool), Result(attributeName=Usage, className=sun.management.MemoryPoolImpl, objDomain=java.lang, typeName=type=MemoryPool,name=PS Survivor Space, values={init=5242880, committed=524288, max=524288, used=327680}, epoch=1473183838906, keyAlias=memorypool)] of query Query(objectName=java.lang:type=MemoryPool,name=*, keys=[], attr=[Usage], typeNames=[], resultAlias=memorypool, useObjDomainAsKey=false, allowDottedKeys=false, useAllTypeNames=false, outputWriterInstances=[BaseOutputWriter(typeNames=[], debugEnabled=false, settings={host=10.143.1.43, port=4242}, valueTransformer=com.googlecode.jmxtrans.model.results.IdentityValueTransformer@35a68c72), BaseOutputWriter(typeNames=[], debugEnabled=false, settings={}, valueTransformer=com.googlecode.jmxtrans.model.results.IdentityValueTransformer@3b6164b6)]) to output writer BaseOutputWriter(typeNames=[], debugEnabled=false, settings={host=10.143.1.43, port=4242}, valueTransformer=com.googlecode.jmxtrans.model.results.IdentityValueTransformer@35a68c72)
java.lang.NullPointerException
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter.processOneMetric(OpenTSDBMessageFormatter.java:196)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter.formatResult(OpenTSDBMessageFormatter.java:159)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter.access$000(OpenTSDBMessageFormatter.java:54)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter$1.apply(OpenTSDBMessageFormatter.java:170)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter$1.apply(OpenTSDBMessageFormatter.java:166)
at com.google.common.collect.Iterators$8.transform(Iterators.java:817)
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:48)
at com.google.common.collect.Iterators$5.hasNext(Iterators.java:569)
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249)
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209)
at com.google.common.collect.FluentIterable.toList(FluentIterable.java:484)
at com.googlecode.jmxtrans.model.output.support.opentsdb.OpenTSDBMessageFormatter.formatResults(OpenTSDBMessageFormatter.java:173)
at com.googlecode.jmxtrans.model.output.OpenTSDBWriter.internalWrite(OpenTSDBWriter.java:116)
at com.googlecode.jmxtrans.model.output.BaseOutputWriter.doWrite(BaseOutputWriter.java:157)
at com.googlecode.jmxtrans.jmx.ResultProcessor$1.run(ResultProcessor.java:58)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=SystemLoadAverage, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={SystemLoadAverage=0.09}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=AvailableProcessors, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={AvailableProcessors=4}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=TotalPhysicalMemorySize, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={TotalPhysicalMemorySize=8252579840}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=FreePhysicalMemorySize, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={FreePhysicalMemorySize=170389504}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=TotalSwapSpaceSize, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={TotalSwapSpaceSize=1073737728}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=FreeSwapSpaceSize, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={FreeSwapSpaceSize=1073737728}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=OpenFileDescriptorCount, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={OpenFileDescriptorCount=83}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=MaxFileDescriptorCount, className=sun.management.OperatingSystemImpl, objDomain=java.lang, typeName=type=OperatingSystem, values={MaxFileDescriptorCount=50000}, epoch=1473183838891, keyAlias=null)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=HeapMemoryUsage, className=sun.management.MemoryImpl, objDomain=java.lang, typeName=type=Memory, values={init=130023424, committed=111673344, max=1834483712, used=21039656}, epoch=1473183838891, keyAlias=heap)
INFO | jvm 1 | 2016/09/06 23:13:58 | Result(attributeName=NonHeapMemoryUsage, className=sun.management.MemoryImpl, objDomain=java.lang, typeName=type=Memory, values={init=2555904, committed=32178176, max=-1, used=30542232}, epoch=1473183838891, keyAlias=heap)
My Config.Json looks like the below:-
{
"servers": [
{
"numQueryThreads": "2",
"host": "xx.xx.xx.xx",
"port": "1099",
"queries": [
{
"outputWriters": [
{
"@class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"@class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"obj": "java.lang:type=OperatingSystem",
"attr": [
"SystemLoadAverage",
"AvailableProcessors",
"TotalPhysicalMemorySize",
"FreePhysicalMemorySize",
"TotalSwapSpaceSize",
"FreeSwapSpaceSize",
"OpenFileDescriptorCount",
"MaxFileDescriptorCount"
]
},
{
"outputWriters": [
{
"@class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"@class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "heap",
"obj": "java.lang:type=Memory",
"attr": [
"HeapMemoryUsage",
"NonHeapMemoryUsage"
]
},
{
"outputWriters": [
{
"@class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"@class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "cmsoldgen",
"obj": "java.lang:name=CMS Old Gen,type=MemoryPool",
"attr": [
"Usage"
]
},
{
"outputWriters": [
{
"@class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"@class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "gc",
"obj": "java.lang:type=GarbageCollector,name=",
"attr": [
"CollectionCount",
"CollectionTime"
]
},
{
"outputWriters": [
{
"@class": "com.googlecode.jmxtrans.model.output.StdOutWriter",
"settings": {}
},
{
"@class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "memorypool",
"obj": "java.lang:type=MemoryPool,name=",
"attr": [
"Usage"
]
},
{
"outputWriters": [
{
"@class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"@class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "threads",
"obj": "java.lang:type=Threading",
"attr": [
"DaemonThreadCount",
"PeakThreadCount",
"ThreadCount",
"TotalStartedThreadCount"
]
},
{
"outputWriters": [
{
"@class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"@class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "tomcat8-connectors",
"obj": "Catalina:type=ThreadPool,name=",
"attr": [
"currentThreadCount",
"currentThreadsBusy"
]
},
{
"outputWriters": [
{
"@class": "com.googlecode.jmxtrans.model.output.StdOutWriter"
},
{
"@class": "com.googlecode.jmxtrans.model.output.OpenTSDBWriter",
"settings": {
"host": "xx.xx.xx.xx",
"port": "4242"
}
}
],
"resultAlias": "tomcat8-requests",
"obj": "Catalina:type=GlobalRequestProcessor,name=",
"attr": [
"bytesReceived",
"bytesSent",
"errorCount",
"processingTime",
"requestCount"
]
}
]
}
]
}
导致此行出错:
.val()
正确删除它会计算行长度(至少在计算区域中)。
至于计数器textarea错误,你刚刚得到jQuery选择器错误。将您的元素$result = $content.val().length;
重命名为set_a_syllable_count
。
还有一个问题 - 当textarea为空时,它会抛出错误,因为您正在检查空的正则表达式结果(set_a_content
,确切地说)。当没有匹配的行时,你只需要防止计数代码执行:
null
固定代码:
if (arrayOfLines !== null) { ... counting code ... }
function $CharCount($input) {
$("[name=set_" + $input + "]").keyup(function() {
var arrayOfLines = $("[name=set_" + $input + "]").val().match(/[^\r\n]+/g);
var tempArr = [];
var $content;
var char;
var $result;
if (arrayOfLines !== null) {
for (var i = 0; i < arrayOfLines.length; i++) {
$content = arrayOfLines[i];
$result = $content.length;
tempArr.push($result);
}
}
$("[name=set_" + $input + "_content]").val(tempArr);
});
}
(function($) {
$CharCount("a");
})(jQuery);
答案 1 :(得分:1)
我的建议是:
function $count_how_many_syllables($input) {
$("[name=set_" + $input + "]").keyup(function (e) {
var arrayOfLines = $("[name=set_" + $input + "]").val().match(/[^\r\n]+/g);
var tempArr = [];
var $charsPerLine;
for (var i = 0; i < arrayOfLines.length; i++) {
$charsPerLine = arrayOfLines[i].length;
tempArr.push($charsPerLine);
}
$("[name=set_" + $input + "_syllable_count]").val(tempArr.join('\n'));
}).trigger('keyup');
}
$(function () {
$count_how_many_syllables("a");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea rows="8" cols="3" class="alignright" name="set_a_syllable_count" readonly="readonly" /></textarea>
<textarea rows="8" cols="30" name="set_a">i would appreciate
any help
at all</textarea>
&#13;
答案 2 :(得分:0)
我有点太慢但是这里有一些代码可以用更少的努力实现同样的效果。
function count() {
var lines = document
.getElementById("area")
.value
.split("\n");
var lengths = [];
for (var i = 0; i < lines.length; i++) {
var lineLength = lines[i].length;
document.write(lineLength+ "</br>");
lengths.push(lineLength);
}
}
<textarea id="area"></textarea>
<br />
<button onclick="count();">Count</button>
答案 3 :(得分:0)