Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 834877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:44:20+00:00 2026-05-15T04:44:20+00:00

I plan to skip the block content which include the start line of MaterializeU4()

  • 0

I plan to skip the block content which include the start line of “MaterializeU4()” with the subroutin() read_block below. But failed.

# Read a constant definition block from a file handle. 
# void return when there is no data left in the file. 
# Otherwise return an array ref containing lines to in the block.  
sub read_block { 
    my $fh = shift; 

    my @lines; 
    my $block_started = 0; 

    while( my $line = <$fh> ) { 

    # how to correct my code below? I don't need the 2nd block content.
 $block_started++ if ( ($line =~ /^(status)/) && (index($line, "MaterializeU4") != 0) ) ;

 if( $block_started ) { 

     last if $line =~ /^\s*$/; 

     push @lines, $line; 
 }

    } 
    return \@lines if @lines;
    return; 
} 

Data as below:

__DATA__ 
status DynTest = <dynamic 100>
vid = 10002
name = "DynTest"
units   = ""

status VIDNAME9000 = <U4 MaterializeU4()>
vid = 9000
name = "VIDNAME9000"
units = "degC"

status DynTest = <U1 100>
vid = 100
name = "Hello"
units   = ""

Output:

  <StatusVariables>
    <SVID logicalName="DynTest" type="L" value="100" vid="10002" name="DynTest" units=""></SVID>
    <SVID logicalName="DynTest" type="L" value="100" vid="100" name="Hello" units=""></SVID>
  </StatusVariables>

[Updated]
I print the value of index($line, "MaterializeU4"), it output 25.
Then I updated the code as below

$block_started++ if ( ($line =~ /^(status)/) && (index($line, "MaterializeU4") != 25)

Now it works.

Any comments are welcome about my practice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:44:21+00:00Added an answer on May 15, 2026 at 4:44 am

    First, using a regex instead of index is probably better since you can tune it to the exact format of status string if you may decide to be stricter than just “substring exists”

    I would suggest as one solution adding a second flag to skip the block contents if it’s a MaterializeU4 block, as follows:

    # 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 $fh = shift; 
        my @lines = (); 
        my $block_started = 0; 
        my $block_ignore = 0;
        while (my $line = <$fh> ) { 
            if ($line =~ /^status.*?((MaterializeU4)?)/) {
                $block_started = 1;
                $block_ignore = 1 if $1;
            }
            last if $line =~ /^\s*$/ && $block_started;
            push @lines, $line unless $block_ignore; 
        } 
        return \@lines if @lines || $block_started;
        return; 
    } 
    

    Here’s a slightly modified sample I tested using codepad.org:

    Code:

    use Data::Dumper;
    my @all_lines = (
      "s 1" ,"b 1" ,""
    , "s MaterializeU4" ,"b 2" ,""
    , "s 3" ,"b 3" ,""
    );
    
    while (@all_lines) {
        my $block = read_block();
        print Data::Dumper->Dump([$block]);
    }
    exit 0;
    
    sub read_block { 
        my @lines = (); 
        my $block_started = 0; 
        my $block_ignore = 0;
        while (my $line = shift @all_lines) { 
            if ($line =~ /^s .*?((MaterializeU4)?)/) {
                $block_started = 1;
                $block_ignore = 1 if $1;
            }
            last if $line =~ /^\s*$/ && $block_started;
            push @lines, $line unless $block_ignore; 
        } 
        return \@lines if @lines || $block_started;
        return; 
    } 
    

    Output:

    $VAR1 = [
              's 1',
              'b 1'
            ];
    $VAR1 = [];
    $VAR1 = [
              's 3',
              'b 3'
            ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I plan to delete an entire table with over 930,000 rows of data. Which
I plan on learning the ins and outs of mod_rewrite, but I have a
I plan to make a Python game server but I can't see how to
We're writing an Android app which we plan to make available on Android devices
I plan to start developing web-service client using Delphi XE. It looks like creating
I plan on buying two domain names for the same site. Depending on which
We plan to have two div as below. The left div we plan to
I plan to run a webserver with some content generated through a Python script.
I plan to include google map using javascript api to show search results for
I plan on using multiple popups on my website. Every popup will be rendered

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.