语法对比:Csh Makefile Perl Tcl Matlab C SV

差异点:

变量类型

数组 列表 哈希 指针/引用

变量使用

赋值  引用  set  = $

结构分句

{} ()  ;

结构控制

if else case switch for while

文件

open close seek   printf scanf   read write

扫描二维码关注公众号,回复: 8764933 查看本文章

子程序

sub/proc

1)Csh Makefile

 

Csh

Makefile

变量

参数

$argv[1]  $argv[2] $argv[*]  

-e  case=100

列表

$array[0]  

obj = obj1 obj2 obj3

数组

哈希/关联数组

 

 

指针/引用

 

 

赋值

 

set xxx = xxx

setenv xxx xxx

set names = ( Tom Dick Fred )

@  n=5+5

xxx = xxx

引用

 

数组引用:$argv[n]

标准输入:$<  

变量存在:$?name

成员个数:$#name

进程号:   $$

$(variable)

逻辑判断

 

&&

||

 

位运算

 

 

 

条件

if else

if ( expression ) then

  ...

else if ( expression ) then

   ...

else

   ...

endif

ifeq ifneq

ifdef ifndef

 

ifeq (...)

 ...

else

 ...

endif

 

 

case

switch ( "$xxx" )

   case xxx:

     ...

     breaksw

   case xxx:

      ...

      breaksw

   default:

      ...

endsw

 

? 

 

 

循环

for

 

for number in 1 2 3 4 ; do \
    ./a.out $$number ; \
done

foreach

foreach variable ( word list )

   ...

end

$(foreach VAR,LIST,CMD 1;CMD 2;)

 

repeat

repeat 3 "echo helloworld"

 

while

while ( expression )

  ...

 continue

  ...

  break

  ...

end

    number=1 ; while [[ $$number -le 10 ]] ; do \
        echo $$number ; \
        ((number = number + 1)) ; \
    done

自动标号

 

objects = file1.o file2.o file3.o

all: $(objects)

$(objects): %.o: %.cpp
        $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

跳转

goto

goto ...

 

文件

fopen

输出重定向到文件为: >
输出重定向并追加到文件为:>>
输入重定向到文件为: <
错误重定向到一个文件:(cmd>/dev/tty) >& errors
输出和错误分别重定向:(cmd > goodstuff)  >& badstuff。

-e 文件存在返回1(.true.)
-r 文件可读返回1,否则返回0
-w ....可写
-x ....可执行
-o 文件属于用户本人返回1
-z 文件长度为0返回1, 否则返回0
-f 文件为普通(plain)文件返回1 
-d 文件为目录文件时返回1

:t (tail)只保留路径名最右边的部分, 而将前面的全部去掉. (相当于basename)
:r (root)去掉从"."(dot)开始的后缀部分.
:e (end)去掉从左边开始一直的"."的前缀部分.
:h (head)去掉最后的一部分, 只保留文件所在的目录名.

 

fclose

 

 

子程序

 

 

 

 

 

 

 

2)Perl Tcl

 

 

Perl

https://www.runoob.com/perl/perl-tutorial.html

Tcl

https://www.yiibai.com/tcl

变量

参数

$ARGV[0] $ARGV[1]

 

列表

@a = qw(abc def ghi)  
@a = ('abc','def','ghi')
@a = (1, 2, 3);

@a = (1..10)
@a = (1..10, 21..30)

set listName {item1 item2 .. itemN }
set listName [list item1 item2 ... itemN]
set listName [split "items string" split_characters]

 

set var {orange blue red green}
puts [lindex $var  1]

数组

set  arrName(index) value

哈希/关联数组

哈希:

%hash

 

$hash{$key} = $value;

my %fruit=(apple=>"fruit",banana=>'fruit'); 

 

二维哈希:

$hash{$key1}{$key2} = $value;

 

foreach my $key1 (keys %hash)

{
   my $hash2 = $hash{$key1};

   foreach my $key2 (keys %$hash2 )
   {        
     print $key1."/t".$key2."/t".$hash2-> {$key2}."/n";   
   }

}

dict set colours  colour1 red 
puts $colours


dict set colours  colour2 green
puts $colours

 

set colours [dict create colour1 "black" colour2 "white"]
puts $colours

指针/引用

$scalarref = \$foo;     # 标量变量引用
$arrayref  = \@ARGV;    # 列表的引用
$hashref   = \%ENV;     # 哈希的引用

 

ref($ref_var)   # 返回引用类型

 

赋值

 

$xxx = xxx;

$xxx = "xxx"

set a 100

引用

 

$xxx

$xxx[0]

$hash{$key}

$hash-> {$key}

$a

逻辑判断

 

>=

lt

gt

eq

 

位运算

 

& | ! ^

 

条件

if else

if (xxx) {
  ...
} elsif {
  ...
} else {
  ...
}

 

unless(boolean_expression){
   # boolean_expression为false执行
} else {
   # boolean_expression为true 执行
}

if {...} {

  ...

} else {

  ...
}

 

case

use Switch;


switch(argument){
   case xxx           { ... }
   case "xxx"        { ... }
   else                  { ... }
}

switch switchingString {

   matchString1 {

      body1   
   }   

   matchString2 {

      body2   
   }   
...   
   matchStringn {

      bodyn   
   }

}

?

Exp1 ? Exp2 : Exp3;

 

循环

for

for( $a = 0; $a < 10; $a = $a + 1 ) {
  ...
}

for { set a 10}  {$a < 20} {incr a} {

   puts "value of a: $a"

}

foreach

foreach $a (@list) {
  ...
}

foreach var {a b c d e f} {
  ...
}

 

foreach var1 {a b c} var2 {d e} {
  ...
}

repeat

 

 

while

while(...) {

  ...

}

 

do
{
   ...
} while ( ... );

 

until(...) {

  ...

}

whiel {...} {
  ...
}

 

break

continue

自动标号

 

 

跳转

goto

goto

continue

next 

last

redo

break

continue

文件

fopen

 <STDIN>

 

open(fin,  "<xxx.txt")  || die "cant oepn file xxx.txt";

while(<fin>)


open(fout, ">xxx.txt");

print fout $xxx;

 

set fid [open "xxx.txt" r]

set fid [open "xxx.txt" w]

 

puts $fid $xxx

fclose

close fin;

close fout;

close fid

子程序

define 

sub sub_routine{

  my @list = @_;

  ...

  return xxx;

  ...

}

 

proc sum {arg1 arg2} {
  set x [expr $arg1+$arg2];
  return $x
}

call

&sub_routine($a, @b);

sum 1 2

 

3)Matlab C SV

 

Matlab

https://www.yiibai.com/matlab

C

https://www.yiibai.com/cprogramming/

SV

http://testbench.in/

变量

参数

 

int main(int argc, char* argv[]);

argc   // 参数个数

argv[1] // 参数1

 

列表

 

 

 

数组

a = [1,2;3,4];

a[0] = 100;

a[0] = 100;

哈希/关联数组

 

 

data_type array_id [ index_type ];

 

bit [20:0] array_b[string];

指针/引用

 

int *ptr;

*ptr = xxx;

 

赋值

 

a = 3;

a[0] = 100;

a[0] = 100;

引用

 

a = b;

a = b;

a = b;

逻辑判断

 

 

 

 

位运算

 

 

 

 

条件

if else

if a>0

  ...

elseif

  ...

else

  ...

end

 

if (...) {

  ...

} else if() {

  ...

} else {

  ...

}

if (...) begin

  ...

end else if() begin

  ...

end else begin

  ...

end

case

switch n
  case xxx
     ...
  case xxx
     ...
  otherwise
     ...
end

switch(...) {
  case xxx:
    ...
    break;

  case xxx:
    ...
    break;

  default:
    ...
}

case(...)

  xxx:   

  xxx:

 default:

endcase

 

 

c =  sel ? a : b;

循环

for

for i=0:8
  ...
end

for i=0:2:8
  ...
end

for i=[1,2,3]
  ...
end

for (int i = 1; i< 10; i++) {
  ...
}

for (i = 1; i< 10; i++)

begin
  ...
end

foreach

 

 

int array1[0:7][0:3];


foreach(array1[i])

  foreach(array1[i][j])begin

     ...

  end
end

repeat

 

 

repeat 5 begin

  ...

end

while

while (xxx)

   ...

end

 

break

continue

while () {
  ...
}

 

do {
  ...
} while ()

 

break
continue
goto

while () begin

  ..

end

 

 

自动标号

 

 

 

跳转

goto

 

goto xxx

disable xxx;

文件

fopen


fid=fopen('d:\char.txt','r');
fid=fopen('d:\char.txt','w');

fseek

 

data = fscanf(fid,format,size);
fprintf(fid,'%s',a);

 

fread
fwrite

FILE *fp;
    
fp = fopen("file.txt", "r");
fp = fopen("file.txt", "w");
fclose(fp);

fseek

 

fscanf

fprintf

 

fread
fwrite

integer fp;
    
fp = fopen("file.txt", "r");
fp = fopen("file.txt", "w");
fclose(fp);

fseek

 

fscanf

fprintf

 

fread
fwrite

fclose

子程序

define

function [output] = f(input)
  ...
end

sub_routine (int a, int b) {

  ...

}

function  sub(reg a, reg b);

   ...

  sub = xxx;

  ...

endfunction

call

[output] = f(input)

sub_routine (a,b);

a = sub(a,b);

 

发布了20 篇原创文章 · 获赞 4 · 访问量 3097

猜你喜欢

转载自blog.csdn.net/niceshotgoodball/article/details/104045947
TCL