[PerlChina] China-pm Digest, Vol 43, Issue 23

cnhack TNT cnhacktnt at gmail.com
Tue Mar 3 19:10:25 PST 2009


用 else 的话, 你得把 /^\s+(.*)$/ 放在 else 块中,再做处理:
我是这样处理的:
-------------------------------------------------
#!/usr/bin/perl;
use strict;

my ($name, $title) = ();

while(<DATA>){

    if (/^(\d+)\s+(.*)$/){

        print "====\n$name\t$title\n=====\n" if $name;
        $name = $1;
        $title = $2;
        next;

    } elsif (/^\s+(.*)$/) {

        $title.= $1;
    }

    print "====\n$name\t$title\n=====\n" if eof;

}


__DATA__
2222  eewweasdds,asdasd.sadsadasdas
      ddasdddddddddddddddddddda
      ddddddddddddddddddddddddddd
2821  easdddddddddddddddddddddddd
2192  288888888888888888888ass0a-das.
      as;dddsaddaa0---------22222222
3030  123456
      789

#-----------------END-----------------

2009/3/3 owen nirvana <freeespeech at gmail.com>

> > my ($name, $title);
> > while (<>){
> > if (/^(\d+)\s+(.*)$) {
> > if ($name) {
> >  // 保存上一个 save $name, $title
> >  // 下面开始处理一个新的
> > $name = '';
> > $title = '';
> > }
> > $name = $1;
> > $title .= $2;
> > } elsif(/^\s+(.*)$/) {
> > $title .= $1;
> > }
> > // save last one
>
> 把自己的老问题翻上来,今天突然发现如果不是elsif, 而是else, 就只有匹配行的第二行会被合并,下一行就不会, why
> gtalk:freeespeech at gmail.com <gtalk%3Afreeespeech at gmail.com>
>
>
>
> 2008/11/18  <china-pm-request at pm.org>:
> > Send China-pm mailing list submissions to
> >        china-pm at pm.org
> >
> > To subscribe or unsubscribe via the World Wide Web, visit
> >        http://mail.pm.org/mailman/listinfo/china-pm
> > or, via email, send a message with subject or body 'help' to
> >        china-pm-request at pm.org
> >
> > You can reach the person managing the list at
> >        china-pm-owner at pm.org
> >
> > When replying, please edit your Subject line so it is more specific
> > than "Re: Contents of China-pm digest..."
> >
> >
> > Today's Topics:
> >
> >   1. Re:  China-pm Digest, Vol 43, Issue 21 (truncatei)
> >
> >
> > ----------------------------------------------------------------------
> >
> > Message: 1
> > Date: Mon, 17 Nov 2008 22:01:40 +0800
> > From: truncatei <truncatei at gmail.com>
> > Subject: Re: [PerlChina] China-pm Digest, Vol 43, Issue 21
> > To: china-pm at pm.org
> > Message-ID:
> >        <c0d775f40811170601m6c2437aeyc2a3c19e7fe0a82e at mail.gmail.com>
> > Content-Type: text/plain; charset="utf-8"
> >
> > 2008/11/17 owen nirvana <freeespeech at gmail.com>
> >
> >> 谢谢,问题是归到上一行这样的操作,我不知道如何用正则表达式完成,那不等于是让表达式再回溯吗
> >> gtalk:freeespeech at gmail.com <gtalk%3Afreeespeech at gmail.com> <
> gtalk%3Afreeespeech at gmail.com <gtalk%253Afreeespeech at gmail.com>>
> >>
> >>
> >> >>
> >> >>
> >> >> 还有一个问题,是关于提取数据的
> >> >>
> >> >> 2222  eewweasdds,asdasd.sadsadasdas
> >> >>          ddasdddddddddddddddddddda
> >> >>          ddddddddddddddddddddddddddd
> >> >> 2821  easdddddddddddddddddddddddd
> >> >> 2192  288888888888888888888ass0a-das.
> >> >>          as;dddsaddaa0---------22222222
> >> >>
> >> >> 诸如此类,左边很好办,可是右边并不限于单行,我该如何把这样的数据提取为  name title的格式存到数据表里呢
> >>
> >> <http://mail.pm.org/mailman/listinfo/china-pm>
> >
> >
> >
> > my ($name, $title);
> > while (<>){
> > if (/^(\d+)\s+(.*)$) {
> > if ($name) {
> >  // 保存上一个 save $name, $title
> >  // 下面开始处理一个新的
> > $name = '';
> > $title = '';
> > }
> > $name = $1;
> > $title .= $2;
> > } elsif(/^\s+(.*)$/) {
> > $title .= $1;
> > }
> > // save last one
> >
> > 好久不写了,上面的代码或许会有些错误,大概的流程整理出来了:
> > 每次遇到数字就开始一条数据,否则视为向当前数据中添加title
> >
> >
> > --
> >
> "老是呆在那里不好,我们随生活需要四处迁徙,多年以来的梦想,至今还未实现,压力堵塞了我们的耳朵,工作生涯到了尽头,还是一无所有,我们应该给后人留下点儿什么,教训是不会刻在纪念碑上的,光荣的命令和指引之手,也可请求,简单地改变一下角度就足够了"
> > -------------- next part --------------
> > An HTML attachment was scrubbed...
> > URL: <
> http://mail.pm.org/pipermail/china-pm/attachments/20081117/e8fdbb3d/attachment-0001.html
> >
> >
> > ------------------------------
> >
> > _______________________________________________
> > China-pm mailing list
> > China-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/china-pm
> >
> > End of China-pm Digest, Vol 43, Issue 23
> > ****************************************
> >
> _______________________________________________
> China-pm mailing list
> China-pm at pm.org
> http://mail.pm.org/mailman/listinfo/china-pm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/china-pm/attachments/20090304/98397bc5/attachment.html>


More information about the China-pm mailing list