🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.perl
2 messages
2 total messages Started by Scottie Thu, 09 Jun 2011 10:09
[RegEx] Parametr opcjonalny w wyrażeniu regularnym
#52250
Author: Scottie
Date: Thu, 09 Jun 2011 10:09
70 lines
4157 bytes
Cześć!
Mam problem z skonstruowaniem poprawnego wyrażenia regularnego. Mam
poniższy kod, a Perl czepia się (Use of uninitialized value in
concatenation (.) or string at /tmp/test.pl line 10) linii nr 10,
która jest równa print "|$backup_compressed|$type_of_backup|\n";
Mój kod:
-----------8<-----------
#!/usr/bin/perl
use warnings;
use strict;

while (<DATA>) {
    my ($backup_compressed, $type_of_backup ) = $_ =~ /
channel.*starting\s(?!piece)(.*)\s(.*)\sdatafile.*/;
    if ( $backup_compressed eq '') {
        $backup_compressed = "not_compressed";
    }
    print "|$backup_compressed|$type_of_backup|\n";
}


__DATA__
channel ch1: starting compressed full datafile backup set
channel ch1: starting piece 1 at 2011-05-30 02:00:08
channel ch2: starting compressed full datafile backup set
channel ch2: starting piece 1 at 2011-05-30 02:00:08
channel ch3: starting compressed full datafile backup set
channel ch3: starting piece 1 at 2011-05-30 02:00:08
channel ch1: starting compressed full datafile backup set
channel ch1: starting piece 1 at 2011-05-30 02:37:19
channel ch1: starting compressed full datafile backup set
channel ch1: starting piece 1 at 2011-05-30 02:37:20
channel ch1: starting compressed archived log backup set
channel ch1: starting piece 1 at 2011-05-30 02:50:57
channel ch2: starting compressed archived log backup set
channel ch2: starting piece 1 at 2011-05-30 02:50:57
channel ch3: starting compressed archived log backup set
channel ch3: starting piece 1 at 2011-05-30 02:50:57
channel ch1: starting full datafile backup set
channel ch1: starting piece 1 at 2011-05-31 02:00:06
channel ch2: starting full datafile backup set
channel ch2: starting piece 1 at 2011-05-31 02:00:06
channel ch3: starting full datafile backup set
channel ch3: starting piece 1 at 2011-05-31 02:00:07
channel ch1: starting full datafile backup set
channel ch1: starting piece 1 at 2011-05-31 02:34:57
channel ch1: starting full datafile backup set
channel ch1: starting piece 1 at 2011-05-31 02:34:58
channel ch1: starting archived log backup set
channel ch1: starting piece 1 at 2011-05-31 02:47:55
channel ch2: starting archived log backup set
channel ch2: starting piece 1 at 2011-05-31 02:47:55
channel ch3: starting archived log backup set
channel ch3: starting piece 1 at 2011-05-31 02:47:55
-----------8<-----------

Wynik, który chciałbym uzyskać to:
Jeżeli linia jest taka => channel ch1: starting compressed full
datafile backup set
to zmienne $backup_compressed = "Yes" i $type_of_backup = "Full"
Natomiast jeżeli linia jest taka => channel ch1: starting full
datafile backup set
to zmienne $backup_compressed = "No" i $type_of_backup = "Full"

Będę zobowiązany za jakąś pomoc. Zwłaszcza w skonstruowaniu poprawnego
RegEx'pa lub innym obejściu (w Perlu?) tego problemu.

Pozdrawiam,
--
Scottie
Re: [RegEx] Parametr opcjonalny w wyrażeniu regularnym
#52251
Author: Maciej Misiak
Date: Thu, 09 Jun 2011 22:06
32 lines
1928 bytes
W dniu 2011-06-09 12:09, Scottie pisze:
> Cześć!
> Mam problem<...>

Problem jest ten, że nie zawsze w linii występuje słowo 'datafile', 
które w Twoim regexpie jest konieczne.
Po poprawieniu regexpa na
/channel.*starting\s(?!piece)(.*)\s(.*)\s(datafile)?.*/
problemem staje się warunek (?!piece), który nie jest spełniony w co 
drugiej linii. I znowu cały regexp się nie dopasowuje, a więc wynikiem 
jest 'uninitialized variable'.

Generalnie ja bym poszedł na łatwiznę:

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

while (<DATA>) {
     if(/starting\s(?!piece)/)
     {
			my ($backup_compressed, $type_of_backup ) = ('No', 'Not Full');
			if(/compressed/i)
				{ $backup_compressed = 'Yes' }
			if(/full/i)
				{ $type_of_backup = 'Full' }
	    print "|$backup_compressed|$type_of_backup|\n";
     }
}

-- 
grizzley
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads