如何删除Perl字符串中的空格?

前端之家收集整理的这篇文章主要介绍了如何删除Perl字符串中的空格?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我声明一个值为’3’的变量$myString(注意空格).
是否有任何功能删除返回值的空格.
有点像SomeFun($myString)然后返回’3′(没有空格).
  1. #!C:\Perl\bin\perl.exe
  2. use strict;
  3. use warnings;
  4. use Data::Dumper;
  5.  
  6. my $fh = \*DATA;
  7.  
  8. print Dumper parse_constant_spec( $fh );
  9.  
  10.  
  11. # Parse a constant spec file.
  12. # Pass in a handle to process.
  13. # As long as it acts like a file handle,it will work.
  14. sub parse_constant_spec {
  15. my $fh = shift;
  16.  
  17. my %spec;
  18.  
  19. # Until file is done:
  20. # Read in a whole block
  21. while( my $block = read_block($fh) ) {
  22.  
  23. # Parse the and return key/value pairs for a hash.
  24. my %constant = parse_block( $block );
  25.  
  26. # Store a ref to the hash in a big hash of all blocks,keyed by constant_name.
  27. $spec{ $constant{const_name} } = \%constant;
  28.  
  29. }
  30.  
  31. # Return ref to big hash with all block data
  32. return \%spec;
  33. }
  34.  
  35. # Read a constant definition block from a file handle.
  36. # void return when there is no data left in the file.
  37. # Otherwise return an array ref containing lines to in the block.
  38. sub read_block {
  39. my $fh = shift;
  40.  
  41. my @lines;
  42. my $block_started = 0;
  43.  
  44. while( my $line = <$fh> ) {
  45.  
  46. $block_started++ if $line =~ /^constant/;
  47.  
  48. if( $block_started ) {
  49.  
  50. last if $line =~ /^\s*$/;
  51.  
  52. push @lines,$line;
  53. }
  54. }
  55.  
  56. return \@lines if @lines;
  57.  
  58. return;
  59. }
  60.  
  61.  
  62. sub parse_block {
  63. my $block = shift;
  64. my ($start_line,@attribs) = @$block;
  65.  
  66. my %constant;
  67.  
  68. # Break down first line:
  69. # First separate assignment from option list.
  70. my ($start_head,$start_tail) = split /=/,$start_line;
  71.  
  72. # work on option list
  73. my @options = split /\s+/,$start_head;
  74.  
  75. # Recover constant_name from options:
  76. $constant{const_name} = pop @options;
  77. $constant{options} = \@options;
  78.  
  79. # Now we parse the value/type specifier
  80. @constant{'type','value' } = parse_type_value_specifier( $start_tail );
  81.  
  82. # Parse attribute lines.
  83. # since we've already got multiple per line,get them all at once.
  84. chomp @attribs;
  85. my $attribs = join ' ',@attribs;
  86.  
  87. # we have one long line of mixed key = "value" or key = <TYPE VALUE>
  88.  
  89. @attribs = $attribs =~ /\s*(\w+\s+=\s+\w+\s+|\w+\s+=\s+".*?"|\w+\s+=\s+<.*?>)\s*/g;
  90.  
  91. for my $attrib ( @attribs ) {
  92. warn "$attrib\n";
  93. my ($name,$value) = split /\s*=\s*/,$attrib;
  94.  
  95. if( $value =~ /^"/ ) {
  96. $value =~ s/^"|"\s*$//g;
  97. }
  98. elsif( $value =~ /^</ ) {
  99. $value = [ parse_type_value_specifier( $start_tail ) ];
  100. }
  101. else {
  102. warn "Bad line";
  103. }
  104.  
  105. $constant{ $name } = $value;
  106. }
  107.  
  108. return %constant;
  109. }
  110.  
  111. sub parse_type_value_specifier {
  112. my $tvs = shift;
  113.  
  114. my ($type,$value) = $tvs =~ /<(\w+)\s+(.*?)>/;
  115.  
  116. return $type,$value;
  117. }
  118.  
  119. __DATA__
  120. constant fixup GemEstabCommDelay = <U2 20>
  121. vid = 6
  122. name = "ESTABLISHCOMMUNICATIONSTIMEOUT"
  123. units = "s"
  124. min = <U2 0>
  125. max = <U2 1800>
  126. default = <U2 20>
  127.  
  128.  
  129. constant fixup private GemConstantFileName = <A "C:\\TMP\\CONST.LOG">
  130. vid = 4
  131. name = "" units = ""
  132.  
  133.  
  134. constant fixup private GemAlarmFileName = <A "C:\\TMP\\ALARM.LOG">
  135. vid = 0
  136. name = ""
  137. units = ""

输出

  1. D:\learning\perl>hello1.pl
  2. vid = 6
  3. Bad line at D:\learning\perl\hello1.pl line 102,<DATA> line 8.
  4. name = "ESTABLISHCOMMUNICATIONSTIMEOUT"
  5. units = "s"
  6. min = <U2 0>
  7. max = <U2 1800>
  8. default = <U2 20>
  9. vid = 4
  10. Bad line at D:\learning\perl\hello1.pl line 102,<DATA> line 13.
  11. name = ""
  12. units = ""
  13. vid = 0
  14. Bad line at D:\learning\perl\hello1.pl line 102,<DATA> line 18.
  15. name = ""
  16. units = ""
  17. $VAR1 = {
  18. 'GemAlarmFileName' => {
  19. 'vid' => '0 ','options' => [
  20. 'constant','fixup','private'
  21. ],'value' => '"C:\\\\TMP\\\\ALARM.LOG"','name' => '','type' => 'A','const_name' => 'GemAlarmFileName','units' => ''
  22. },'GemEstabCommDelay' => {
  23. 'vid' => '6 ','options' => [
  24. 'constant','fixup'
  25. ],'value' => '20','min' => [
  26. 'U2','20'
  27. ],'name' => 'ESTABLISHCOMMUNICATIONSTIMEOUT','max' => [
  28. 'U2','default' => [
  29. 'U2','20'
  30. ],'type' => 'U2','units' => 's','const_name' => 'GemEstabCommDelay'
  31. },'GemConstantFileName' => {
  32. 'vid' => '4 ','options' => [
  33. 'constant','private'
  34. ],'value' => '"C:\\\\TMP\\\\CONST.LOG"','const_name' => 'GemConstantFileName','units' => ''
  35. }
  36. };
  37.  
  38. D:\learning\perl>

你可以注意到’vid’=> ‘0’,(注意空格)

上面的代码the answer.我正在研究它.

猜你在找的Perl相关文章