标签: scalar

在 C 中将数组分配给 int

我用C编写了以下代码

int main(){
    int a = {1, 2, 3};
}
Run Code Online (Sandbox Code Playgroud)

看起来分配的变量(在本例中为 a)总是采用第一个数组元素的值。现在我想知道其他数组元素是否被丢弃,或者在a之后写入内存,从而导致缓冲区溢出。

c arrays scalar initialization braced-init-list

2
推荐指数
1
解决办法
170
查看次数

OpenCV乘以标量和矩阵

我试图找到最简单的添加方法,用opencv 2.0 cv::Mat类减去标量值.

大多数现有函数仅允许矩阵 - 矩阵和矩阵 - 标量运算.

我正在寻找一个标量矩阵运算.

我目前正在通过创建具有相同标量值的临时矩阵并执行所需的算术运算来完成它.下面..实例 cv::Mat确实是基于场副本场cv::Mat课,我会说,我不会需要显式的复制场cv::Matcv::Mat,是上面显示的代码将是绰绰有余做出的克隆cv::Mat类.也就是说,以下代码是多余的:

Mat M(Size(100,100), CV_8U);
Mat temp = Mat::ones(100, 100, CV_8U)*255; 
M = temp-M;
Run Code Online (Sandbox Code Playgroud)

我对吗?

我知道克隆对象的引用会自动指向原始对象的引用指向的位置,我只是不确定具体的值类型会发生什么.如果有人能够清楚地说明什么cv::Mat是算法规范(用简单的语言)那就太好了.

math scalar opencv matrix operation

1
推荐指数
1
解决办法
2万
查看次数

Perl:如何将文本附加到标量

我在Perl中有一个子例程,它将读取一个哈希并打印出哈希中的所有键值对.但是,每次有一个键时,我不需要经过foreach循环和打印,而是需要将结果添加到一个标量中,然后在结尾处返回带有组合结果的标量.

在Java中我记得你可以轻松地将其他文本添加到变量中,但我不知道如何在Perl中执行此操作.

有什么想法吗?我将在下面添加我的打印代码,但我基本上想要将其添加到标量并在结尾处返回合并的标量(让我们说$output)

sub printSongs
{
    print "Song Database\n\n";
    foreach $key (keys %songList)
    {
       print "Song Title: $key ---- Duration: $songList{$key}\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:我试图搜索这个答案,因为它应该相对简单,但找不到任何东西.不确定追加是否是最好的词.

perl scalar return append add

1
推荐指数
1
解决办法
1673
查看次数

wordlist.pl第11行的perl语法错误,靠近")$ fh"

my $fn= "words.txt";
open ($fn), $file;
if (! -e "$fh") $fh="STDIN";
while (<$fn>){;
    my $total_words = @words; #All word count
    my %count;
    $count{$_}++ for @words; # Here are the counts
    my $uniq_words  = scalar keys %count; # Number of uniq words
}
# Print sorted by frequency

print "$_\t$count{$_}" for (sort { $count{$b} <=> $count{$a} } keys %count);

close FILE;
exit 0
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

Scalar found where operator expected at wordlist.pl line 8, near ") $fh"
        (Missing operator before $fh?)
syntax …
Run Code Online (Sandbox Code Playgroud)

syntax perl scalar operator-keyword

1
推荐指数
1
解决办法
320
查看次数

如何在大文件(Perl)中读取后逐行读取SCALAR?

我正在SCALAR使用以下方法将文件读入a :

open FILE, "<", $filename or error_out("Error opening $filename");
read FILE, my $buffer, -s $filename;    
close FILE;
Run Code Online (Sandbox Code Playgroud)

我不确定如何考虑"新" $buffer/ SCALAR类型.它是一个字符串?数组?如何逐行完成?

perl scalar text-files readfile

1
推荐指数
1
解决办法
566
查看次数

Perl:直接为散列分配标量

在查看一些Perl代码时,我遇到了将chr的输出直接赋值给hash:

local %str = chr(shift);
Run Code Online (Sandbox Code Playgroud)

感到困惑的是代码工作正常,这让我意识到以下几点:

perl -le 'my $jurso = 23; print $jurso;'
23

perl -le 'my %jurso = 23; print %jurso;'
23

perl -le 'my @jurso = 23; print @jurso;'
23
Run Code Online (Sandbox Code Playgroud)

我期望将标量直接分配给散列或数组以导致错误.有人可以解释为什么jurso变量表现得像标量而不管使用的sigil?

谢谢.

perl hash scalar sigils variable-assignment

1
推荐指数
1
解决办法
161
查看次数

如何正确使用 Python 标量?

>>> numpy.sin(range(11))
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025
   -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.4121184
   -0.54402111])
>>> numpy.array(range(11))*2
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20]) 
>>> str(numpy.array(range(11))... )
'[ 0  1  2  3  4  5  6  7  8  9 10]'
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] 与numpy.sin(range(11)) 或 numpy.array(range(11))*2 等 python 标量的概念?

我可以通过以下方式做到这一点:

>>> s=[]
>>> [s.append(str(i)) for i in range(11)]
>>> s
['0', '1', '2', '3', '4', …
Run Code Online (Sandbox Code Playgroud)

python scalar numpy

1
推荐指数
1
解决办法
149
查看次数

如何在Perl中定义匿名标量引用?

如何在Perl中正确定义匿名标量引用?

my $scalar_ref = ?;

my $array_ref = [];

my $hash_ref = {};

perl scalar reference

1
推荐指数
1
解决办法
1086
查看次数

从PHP V7.2.0开始,"数组解除引用"如何处理类型为boolean/integer/float/string的标量值?

我使用的是PHP 7.2.我从PHP手册数组章节中看到了以下注释

取消引用不是字符串的标量值的数组静默产生NULL,即不发出错误消息.

我理解如何取消引用数组文字,但我无法理解"数组解除引用"如何在boolean/integer/float/string类型的标量值上工作?

如果你看一下PHP手册本身的代码示例,你可以注意到矛盾,因为根据手册,整数类型的值不是默默地产生NULL.

<?php
function getArray() {
    return array(1, 2, 3);
}
$secondElement = getArray()[1];
var_dump($secondElement); // int(2)
//According to the manual I expected it to be NULL as it's not of type string
Run Code Online (Sandbox Code Playgroud)

如何取消引用boolean/integer/float类型的标量值与取消引用string类型的值不同?

php arrays scalar null dereference

1
推荐指数
1
解决办法
244
查看次数

在Perl中,为什么在语句上加上“ || die”会导致它在标量上下文中求值?

最终,我发现我的代码在标量上下文而不是列表上下文中得到评估,即使我在赋值周围有()。

  • 第一个问题是为什么在表达式/赋值上加上“ || die ...”会导致它在标量上下文中求值?

  • 2,在进行列表分配时是否可以使用“ || die ....”成语/等效词?

这是演示该问题的示例代码。

#!/usr/bin/perl
use strict;
use warnings;

use Data::Dumper qw(Dumper);

my $h1 = {
    var => "1",
    bar => "1",
    baz => "1",
};

my $h2 = {
    var => "2",
    bar => "2",
    baz => "2",
};

my $ds;
$ds->{rules} = [$h1,$h2];

print "TEST1\n";
print Dumper($ds);

print "TEST2\n";
my (@processes) = @{$ds->{rules}};
print Dumper(\@processes);
print "@processes\n";

print "TEST3\n";
(@processes) = @{$ds->{rules}} || die "unable to get rules form config.. \n";
print …
Run Code Online (Sandbox Code Playgroud)

perl scalar list variable-assignment

1
推荐指数
1
解决办法
63
查看次数