Nano's Blog

06/03/2010

Perl Practice Scripts Collection

Filed under: programming — Tags: , — nano @ 04:57

test1.pl

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

use Data::Dumper;

my $s;

$s="status MaterializeU4";

print (($s =~ /s\s.*?((MaterializeU4)?)/) ? "Match\n" : "No Match\n");

$s="status MaterializeU4";

print (($s =~ /s\s.*?((MaterializeU4)?)/) ? "Match\n" : "No Match\n");

$s="statusaa dMaterializeU4";

print (($s =~ /s\s.*?((MaterializeU4)?)/) ? "Match\n" : "No Match\n");

$s="status < MaterializeU4>";

print (($s =~ /s\s.*?((MaterializeU4)?)/) ? "Match\n" : "No Match\n");

$s="status VIDNAME9000 = <U4 MaterializeU4()>";

print (($s =~ /s\s.*?((MaterializeU4)?)/) ? "Match\n" : "No Match\n");

# I inserted another one below
$s="status VIDNAME9000 = <U4 MaterializeU4()>";
print (($s =~ /^status .*?((MaterializeU4)?)/) ? "Match\n" : "No Match\n");

output

Match
Match
No Match
Match
Match
Match

test2.pl

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

use Data::Dumper;
my @all_lines = (
 "s 1"
,"b 1"
,""
, "status VIDNAME9000 = <U4 MaterializeU4()>"
,"b 2"
,""
, "s 3"
,"b 3"
,""

);

while (@all_lines) {
    my @block = read_block();
    print Data::Dumper->Dump([\@block]);
}
exit 0;

# Read a constant definition block from a file handle.
# void return when there is no data left in the file.
# Empty return for skippable (Materialize4U) block!!!
# Otherwise return an array ref containing lines to in the block.
sub read_block {
    my @lines = ();
    my $block_started = 0;
    my $block_ignore = 0;
    while (my $line = shift @all_lines) {
        if ($line =~ /s\s.*?((MaterializeU4)?)/){
            $block_started = 1;   # why can't execute
            $block_ignore = 1 if $1; # or is it #1?
        }
        last if $line =~ /^\s*$/ && $block_started;
        push @lines, $line unless $block_ignore;
    }
    return \@lines if @lines;
    return;
}

output

$VAR1 = [
          [
            's 1',
            'b 1'
          ]
        ];
$VAR1 = [
          [
            'status VIDNAME9000 = <U4 MaterializeU4()>',
            'b 2'
          ]
        ];
$VAR1 = [
          [
            's 3',
            'b 3'
          ]
        ];

test3.pl

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

use Data::Dumper;
my @all_lines = (
 "s 1"
,"b 1"
,""
, "status VIDNAME9000 = <U4 MaterializeU4()>"
,"b 2"
,""
, "s 3"
,"b 3"
,""

);

while (@all_lines) {
    my @block = read_block();
    print Data::Dumper->Dump([\@block]);
}
exit 0;

# Read a constant definition block from a file handle.
# void return when there is no data left in the file.
# Empty return for skippable (Materialize4U) block!!!
# Otherwise return an array ref containing lines to in the block.
sub read_block {
    my @lines = ();
    my $block_started = 0;
    my $block_ignore = 0;
    while (my $line = shift @all_lines) {
        if ($line =~ /^status .*?((MaterializeU4)?)/) {
            $block_started = 1;   # why can't execute
            $block_ignore = 1 if $1; # or is it #1?
        }
        last if $line =~ /^\s*$/ && $block_started;
        push @lines, $line unless $block_ignore;
    }
    return \@lines if @lines;
    return;
}

output

$VAR1 = [
          [
            's 1',
            'b 1'
          ]
        ];
$VAR1 = [
          [
            'status VIDNAME9000 = <U4 MaterializeU4()>',
            'b 2'
          ]
        ];
$VAR1 = [
          [
            's 3',
            'b 3'
          ]
        ];

Why test2.pl, test3.pl can’t skip the 2nd block content? thanks.

test4.pl – final

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

use Data::Dumper;
my @all_lines = (
 "s 1"
,"b 1"
,""
, "status VIDNAME9000 = <U4 MaterializeU4()>"
,"b 2"
,""
, "s 3"
,"b 3"
,""

);

while (@all_lines) {
    my @block = read_block();
    print Data::Dumper->Dump([\@block]);
}
exit 0;

# Read a constant definition block from a file handle.
# void return when there is no data left in the file.
# Empty return for skippable (Materialize4U) block!!!
# Otherwise return an array ref containing lines to in the block.
sub read_block {
    my @lines = ();
    my $block_started = 0;
    my $block_ignore = 0;
    while (my $line = shift @all_lines) {
        # original expression as below comment.
        # if ($line =~ /status\s.*?((MaterializeU4)?)/){
        if ($line =~ /^status\s.*?((MaterializeU4))/){
            $block_started = 1;   # why can't execute
            $block_ignore = 1 if $1; # or is it #1?
        }
        last if $line =~ /^\s*$/ && $block_started;
        push @lines, $line unless $block_ignore;
    }
    return \@lines if @lines;
    return;
}

output:

$VAR1 = [
          [
            's 1',
            'b 1'
          ]
        ];
$VAR1 = [];
$VAR1 = [
          [
            's 3',
            'b 3'
          ]
        ];

Powered by WordPress