打印标量值中保存的文本的前五行

时间:2014-06-10 04:30:27

标签: regex perl printing lines

我操纵了标量值中的文本,但是现在我只想保留五行代码并打印出来。我不确定如何去做这件事。

use WWW::Wikipedia;

my $wiki = WWW::Wikipedia->new();

## search for 'perl' 

my $result = $wiki->search( 'perl' );

## if the entry has some text print it out

## This is where I want the five lines to
##  be printed

if ( $result->text() ) { 
    my $rawtext = $result->text();
    print $result->text();
}

## list any related items we can look up
print join( "\n", $result->related() );

2 个答案:

答案 0 :(得分:1)

这样做的一种方式,不一定是最好的方法:

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

my $var = "Line of data\n" x 8;  # Generate sample data in single scalar variable

print "Before: <<$var>>\n";

my @lines;

@lines[0..4] = split /\n/, $var;  # Capture just the first 5 lines

print "After:  <<$_>>\n" for @lines;

示例输出:

Before: <<Line of data
Line of data
Line of data
Line of data
Line of data
Line of data
Line of data
Line of data
>>
After:  <<Line of data>>
After:  <<Line of data>>
After:  <<Line of data>>
After:  <<Line of data>>
After:  <<Line of data>>

更新问题后

安装WWW::Wikipedia之后,这段代码的简单修改对我有用(Perl 5.18.1在Ubuntu 12.04 LTS衍生版本上)。我认为这是反例的证据,证明您的"it won't work for me"声明不正确 - 或者,至少,您尝试的任何内容(您都没有显示)不是正确的尝试。请注意,此代码包含suggested的改进Pierre

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

use WWW::Wikipedia;

my $wiki = WWW::Wikipedia->new();

## search for 'perl' 

my $result = $wiki->search( 'perl' );

## if the entry has some text print it out

## This is where I want the five lines to
##  be printed

if ( $result->text() ) { 
    my $rawtext = $result->text();
    print "Raw: [[$rawtext]]\n";
    $rawtext =~ s/\n\n+/\n/g;  # Remove repeated newlines
    $rawtext =~ s/^\n//;       # Remove leading newlines
    my @lines;
    @lines[0..4] = split "\n", $rawtext, 6;
    print "Line: <<$_>>\n" for @lines;
}

输出:

Raw: [[

{{Infobox programming language
| name = Perl logo = paradigm = multi-paradigm: functional, imperative, object-
| oriented (class-based), reflective, procedural, Event-driven, generic year =
| 1987 designer = Larry Wall developer = Larry Wall latest_release_version =
| 5.20.0<ref></ref> latest_release_date = latest preview version =
| 5.21.0<ref></ref> latest preview date = turing-complete = Yes typing = Dynamic
| influenced_by = AWK, Smalltalk 80, Lisp, C, C++, sed, Unix shell, Pascal
| influenced = Python, PHP, Ruby, ECMAScript, LPC, Windows PowerShell,
| JavaScript, Falcon, Perl 6, Qore programming_language = C operating_system =
| Cross-platform license = GNU General Public License or Artistic License<ref
| name='licensing'></ref> website = file_ext = .pl .pm .t .pod wikibooks = Perl
| Programming
}}

'Perl' is a family of high-level, general-purpose, interpreted, dynamic
programming languages. The languages in this family include Perl 5 and Perl
6.<ref></ref>

Though Perl is not officially an acronym,<ref></ref> there are various
backronyms in use, such as: Practical [[data extraction|Extraction]] and
Reporting Language.<ref></ref> Perl was originally developed by Larry Wall in
1987 as a general-purpose Unix scripting language to make report processing
easier.<ref name='sheppard00'></ref> Since then, it has undergone many changes
and revisions. The latest major stable revision of Perl 5 is 5.20, released in
May 2014. Perl 6, which began as a redesign of Perl 5 in 2000, eventually
evolved into a separate language. Both languages continue to be developed
independently by different development teams and liberally borrow ideas from
one another.

The Perl languages borrow features from other programming languages including C,
shell scripting (sh), AWK, and sed.<ref name="perltimeline"></ref> They provide
powerful text processing facilities without the arbitrary data-length limits of
many contemporary Unix commandline tools,<ref name="programmingperl"></ref>
facilitating easy manipulation of text files. Perl 5 gained widespread
popularity in the late 1990s as a CGI scripting language, in part due to its
parsing abilities.<ref name='roderick02'></ref>

In addition to CGI, Perl 5 is used for graphics programming, system
administration, network programming, finance, bioinformatics, and other
applications. It is nicknamed "the Swiss Army chainsaw <!-- Note to editors.
This should read *chainsaw*, not knife. Check the ref -->of scripting languages"
because of its flexibility and power,<ref></ref> and possibly also because of
its "ugliness".<ref></ref> In 1998, it was also referred to as the "duct tape
that holds the Internet together", in reference to both its ubiquitous use as a
glue language and its inelegance.<ref name='leonard98'></ref>
]]
Line: <<{{Infobox programming language>>
Line: <<| name = Perl logo = paradigm = multi-paradigm: functional, imperative, object->>
Line: <<| oriented (class-based), reflective, procedural, Event-driven, generic year =>>
Line: <<| 1987 designer = Larry Wall developer = Larry Wall latest_release_version =>>
Line: <<| 5.20.0<ref></ref> latest_release_date = latest preview version =>>

答案 1 :(得分:1)

如果您需要显式显示五行数据,则可以使用以下正则表达式/((?:.*\n){5})/

use strict;
use warnings;

my $var = '';
$var .= "Line $_ of data\n" for (1..10);

print "Before: <<$var>>\n";

if ($var =~ /((?:.*\n){5})/) {
    print "Five lines: <<$1>>\n";
} else {
    warn "No match, less than 5 lines";
}

输出:

Before: <<Line 1 of data
Line 2 of data
Line 3 of data
Line 4 of data
Line 5 of data
Line 6 of data
Line 7 of data
Line 8 of data
Line 9 of data
Line 10 of data
>>
Five lines: <<Line 1 of data
Line 2 of data
Line 3 of data
Line 4 of data
Line 5 of data
>>

如果您希望在可用的行数少于5行时始终匹配,则只需稍微调整正则表达式:

if ($var =~ /((?:.*\n?){1,5})/) {