String 为什么我不能打印很长的字符串?
我正在编写一个搜索kml文件的Perl脚本,需要打印一行很长的经纬度坐标。以下脚本成功地找到了我要查找的字符串,但只打印了一个空行,而不是字符串的值:String 为什么我不能打印很长的字符串?,string,perl,String,Perl,我正在编写一个搜索kml文件的Perl脚本,需要打印一行很长的经纬度坐标。以下脚本成功地找到了我要查找的字符串,但只打印了一个空行,而不是字符串的值: #!/usr/bin/perl # Strips unsupported tags out of a QGIS-generated kml and writes a new one $file = $ARGV[0]; # read existing kml file open( INFO, $file ); # Open the file
#!/usr/bin/perl
# Strips unsupported tags out of a QGIS-generated kml and writes a new one
$file = $ARGV[0];
# read existing kml file
open( INFO, $file ); # Open the file
@lines = <INFO>; # Read it into an array
close(INFO); # Close the file
#print @lines; # Print the array
$x = 0;
$coord_string = "<coordinates>";
# go through each line looking for above string
foreach $line (@lines) {
$x++;
if ( $x > 12 ) {
if ( $line =~ $coord_string ) {
$thisCooordString = $line;
$var_startX = $x;
print "Found coord string: $thisCoordString\n";
print " on line: $var_startX\n";
}
}
}
Perl中字符串的最大长度是否有上限?此文件中最长的行约为151000个字符。我已验证文件中的所有行都已成功读取。您将变量名“两个os”与“三个os”拼写错误: 在脚本中添加use strict和use warnings以防止此类错误。始终在每个perl脚本中包含和 如果您这样做了,您会收到以下错误消息,提示您出现错误:
Global symbol "$thisCoordString" requires explicit package name
添加这些杂注并简化代码会导致以下结果:
#!/usr/bin/env perl
# Strips unsupported tags out of a QGIS-generated kml and writes a new one
use strict;
use warnings;
local @ARGV = 'HUC8short.kml';
while (<>) {
if ( $. > 12 && /<coordinates>/ ) {
print "Found coord string: $_\n";
print " on line: $.\n";
}
}
您甚至可以尝试使用perl one行程序,如下所示: windows命令提示符上的Perl一行程序: unix提示符上的Perl一行程序:
正如其他人所指出的,你需要。不,您必须始终使用和 如果使用strict,则会收到一条错误消息,告诉您变量$thisCoordString或$thisCoordString未使用my声明。使用“警告”会警告您正在打印未定义的字符串 您的整个程序都是用非常古老和过时的Perl编程风格编写的。这是大约二十年前我在Perl 3.0中编写的程序类型。从那时起,Perl发生了很大的变化,使用较新的语法将允许您编写更易于阅读和维护的程序 以下是用更现代的语法编写的基本程序:
#! /usr/bin/env perl
#
use strict; # Lets you know when you misspell variable names
use warnings; # Warns of issues (using undefined variables
use feature qw(say); # Let's you use 'say' instead of 'print' (No \n needed)
use autodie; # Program automatically dies on bad file operations
use IO::File; # Lots of nice file activity.
# Make Constants constant
use constant {
COORD_STRING => qr/<coordinates>/, # qr is a regular expression quoted string
};
my $file = shift;
# read existing kml file
open my $fh, '<', $file; # Three part open with scalar filehandle
while ( my $line = <$fh> ) {
chomp $line; # Always "chomp" on read
next unless $line =~ COORD_STRING; #Skip non-coord lines
say "Found coord string: $line";
say " on line: " . $fh->input_line_number;
}
close $fh;
许多Perl开发人员都是自学成才的。这并没有什么错,但许多人是从看别人过时的代码、阅读旧的Perl手册或从上世纪90年代从别人那里学习Perl的开发人员那里学习Perl的
所以,买一些书来学习新的语法。你可能还想学习一些能引导你学习的东西。引用和OO Perl将允许您编写更长、更复杂的程序。哎呀,我错过了这一点,真让人尴尬。谢谢。只要将这些行添加到脚本中,您就再也不会犯这种错误了。@MeganMallard在没有严格警告的情况下编写Perl代码是一个非常糟糕的主意,我甚至不会将其推荐给非常熟练的用户,当然也不会推荐给新用户。不使用这些pragmas不会使错误消失,它们只是隐藏起来。
#!/usr/bin/env perl
# Strips unsupported tags out of a QGIS-generated kml and writes a new one
use strict;
use warnings;
local @ARGV = 'HUC8short.kml';
while (<>) {
if ( $. > 12 && /<coordinates>/ ) {
print "Found coord string: $_\n";
print " on line: $.\n";
}
}
perl -lne "if($_ =~ /<coordinates>/is && $. > 12) { print \"Found coord string : $_ \n"; print \" on line : $. \n\";}" HUC8short.kml
perl -lne 'if($_ =~ /<coordinates>/is && $. > 12) { print "Found coord string : $_ \n"; print " on line : $. \n";}' HUC8short.kml
#! /usr/bin/env perl
#
use strict; # Lets you know when you misspell variable names
use warnings; # Warns of issues (using undefined variables
use feature qw(say); # Let's you use 'say' instead of 'print' (No \n needed)
use autodie; # Program automatically dies on bad file operations
use IO::File; # Lots of nice file activity.
# Make Constants constant
use constant {
COORD_STRING => qr/<coordinates>/, # qr is a regular expression quoted string
};
my $file = shift;
# read existing kml file
open my $fh, '<', $file; # Three part open with scalar filehandle
while ( my $line = <$fh> ) {
chomp $line; # Always "chomp" on read
next unless $line =~ COORD_STRING; #Skip non-coord lines
say "Found coord string: $line";
say " on line: " . $fh->input_line_number;
}
close $fh;