Some parts of this page were machine translated.
Powered by Yandex.Translate
http://translate.yandex.com/
에서는 Perl 프로그래밍 언어가 내장된 기능 chomp()
.
이 기능을 사용할 수 있습니다 작업을 할 때 문자열입니다.
가장 간단한 경우,함수 chomp()
마지막 제거
호 \n
AC
는 전달에서 인수 기능.
chomp()
에서는 Perl
에서는 Perl 프로그래밍 언어가 내장된 기능 chomp()
.
이 기능을 사용할 수 있습니다 작업을 할 때 문자열입니다.
가장 간단한 경우,함수 chomp()
마지막 제거
호 \n
AC
는 전달에서 인수 기능.
예를 들어 다음과 같습니다.
#!/usr/bin/perl
my $string = "ASDF\n";
chomp($string);
print "'$string'";
이 프로그램에 텍스트가 표시됩 'ASDF'
.
\n
,값 변경되지 않습니다
\n
끝에서,그것은 삭제됩니다만 문자 \n
는 경우 기능 chomp
전달되지 않은 인수,함수 작품과 함께 기본 변수 $_
:
#!/usr/bin/perl
$_ = "123\n";
chomp;
print "'$_'";
이 경우에는 경우는 변수 $_
은 undef
고 사용 use warnings;
것,경고:
#!/usr/bin/perl
use strict;
use warnings;
chomp;
Use of uninitialized value $_ in scalar chomp at script.pl line 6.
기능 chomp()
전달할 수 있습니다 스칼라,배열,해시입니다. 의 경우에 해시
의 기능과 함께 작동 값의 키 키를 자신이 변하지 않습니다.
이 인수의 함수 chomp()
항상 변수입니다. 는 경우
을 전달하려고 이 함수 문자열을,그것은 오류로그램 실행
중지됩니다:
#!/usr/bin/perl
chomp("ASDF\n"); # Error!
Can't modify constant item in chomp at script.pl line 3, near ""ASDF\n")"
Execution of script.pl aborted due to compilation errors.
기능 chomp()
항상 정수를 반환합보다 크거나 같은 0
.
는 경우에 반환 0 은
이 의미는 없었습니다.
수상 0
는 방법을 많은 것을 의미는 그것을 변경합니다.
의 예는 다음과 같은 상황을 때 기능 chomp()
번호를 반환합 2
:
#!/usr/bin/perl
my @arr = ("ASDF\n", "QWERTY\n");
print chomp(@arr);
여기에 예를 사용하는 반환 값을 성취하는 다른 코드가 있다면 대체 또는 교체되지 않았:
#!/usr/bin/perl
use feature qw(say);
my $string = "ASDF\n";
if (chomp($string)) {
say 'Removed \n';
} else {
say 'String is unchanged';
}
print "'$string'";
$/
기능 chomp()
제거 라인의 끝는 기호에 포함된 글로벌
변 $/
.
기본적으로,이 변수는 기호를 포함 \n
. 하지만 당신은 게시할 수 있습에서 이
변수의 몇 가지 다른 상징이며 그런 다음 기능 chomp()
이 제거됩니다.
예를 들어 다음과 같습니다.
#!/usr/bin/perl
$/ = "F";
my $string = "ASDF";
chomp($string);
print "'$string'";
프로그램에 텍스트가 표시됩니다 'ASD'
.
이 경우에는 경우는 변수입니다면 빈 문자열($/ = '';
),
다음 chomp()
를 제거 한 후행 \n
,그리고 모든 문자 \n
의 끝에서 행이 있습니다.
매우 자주의 기능 chomp()
가 사용하는 라인으로 라인을 읽
서 파일입니다. 문자열 처리를 위해 파일에서 그것은 종종 편리하게 이러한 선지
그것은 상징 \n
의 끝 부분에 라인:
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use open qw(:std :utf8);
my $file_name = 'a.csv';
open FILE, '<', $file_name or die $!;
while my $line (<FILE>) {
chomp($line);
print "Parsing line $line";
}
\n
의 끝 부분에 라인
일반적으로 기능의 chomp()
제거 하나의 문자 \n
의 끝 부분에 라인:
#!/usr/bin/perl
my $string = "Line1\nLine2\n\n\n";
chomp($string);
print "'$string'";
결과:
'Line1
Line2
'
방법은 여러 가지가 있는 방법을 제거할 수 있습니다 모든 기호 \n
의 끝 부분에 라인입니다.
여기에 예를 들어 정규 표현식을 사용하여:
#!/usr/bin/perl
my $string = "Line1\nLine2\n\n\n";
$string =~ s/\n*$//;
print "'$string'";
또 다른 방법을 모두 제거 후행 공백은 값을 설정
변 $/
빈 문자열:
#!/usr/bin/perl
$/ = '';
my $string = "Line1\nLine2\n\n\n";
chomp($string);
print "'$string'";
이 표준 정규표현식을 사용은 더 나은:그것은 명확하고 변경되지 않는 글로벌 변수에 영향을 미칠 수 있는 코드는 다른 의 부분 프로그램입니다.
여기에 출력하는 명령의 perldoc -f chomp
:
chomp VARIABLE
chomp( LIST )
chomp This safer version of "chop" removes any trailing string that
corresponds to the current value of $/ (also known as
$INPUT_RECORD_SEPARATOR in the "English" module). It returns
the total number of characters removed from all its arguments.
It's often used to remove the newline from the end of an input
record when you're worried that the final record may be missing
its newline. When in paragraph mode ("$/ = """), it removes
all trailing newlines from the string. When in slurp mode ("$/
= undef") or fixed-length record mode ($/ is a reference to an
integer or the like; see perlvar) chomp() won't remove
anything. If VARIABLE is omitted, it chomps $_. Example:
while (<>) {
chomp; # avoid \n on last field
@array = split(/:/);
# ...
}
If VARIABLE is a hash, it chomps the hash's values, but not its
keys.
You can actually chomp anything that's an lvalue, including an
assignment:
chomp($cwd = `pwd`);
chomp($answer = <STDIN>);
If you chomp a list, each element is chomped, and the total
number of characters removed is returned.
Note that parentheses are necessary when you're chomping
anything that is not a simple variable. This is because "chomp
$cwd = `pwd`;" is interpreted as "(chomp $cwd) = `pwd`;",
rather than as "chomp( $cwd = `pwd` )" which you might expect.
Similarly, "chomp $a, $b" is interpreted as "chomp($a), $b"
rather than as "chomp($a, $b)".