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 6214815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:56:38+00:00 2026-05-24T06:56:38+00:00

My problem is that I am not able to figure out that why my

  • 0

My problem is that I am not able to figure out that why my code is taking each of the line from the file as one element of an array instead of taking the whole record starting from AD to SS as one element of the array. As you can see that my file is starting from AD and ending at SS which is same for all the followed lines in the data. But I want to make the array having elements starting from AD to SS which will be having all the lines in between AD to SS that is BC….,EG…., FA…..etc.not each line as an element. I tried my way and get the same file as such.Could anyone check my code. Thanks in advance.

AD uuu23

BC jjj

EG iii

FA vvv

SS

AD hhh25

BC kkk

EG ppp

FA aaa

SS

AD ttt26

BC xxx

FA rrr

SS

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

 my $ifh;
 my $line = '';
 my @data;

 my $ifn  = "fac.txt";

 open ($ifh, "<$ifn") || die "can't open $ifn";
 my $a = "AD  "; 
 my $b = "SS ";
 my $_ = " ";
 while ($line = <$ifh>)
 {
 chomp 
 if ($line =~ m/$a/g); {
  $line = $_;

  push @data, $line;

 while ($line = <$ifh>) 
{
$line .= $_;

push @data, $line;

last if 
($line =~ m/$b/g);
}

}
push @data, $line; }


print @data;
  • 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-24T06:56:39+00:00Added an answer on May 24, 2026 at 6:56 am

    Here’s a way to accomplish reading the records into an array, with newlines removed:

    Code:

    use strict;
    use warnings;
    use autodie;
    
    my @data;
    my $record;
    my $file = "fac.txt";
    open my $fh, '<', $file;
    
    while (<$fh>) {
        chomp;
        if (/^AD /) { # new record starts
            $record = $_;
            while (<$fh>) {
                chomp;
                $record .= $_;
                last if /^SS\s*/;
            }
            push @data, $record;
        } else { die "Data outside record: $_" }
    }
    
    use Data::Dumper;
    print Dumper \@data;
    

    Output:

    $VAR1 = [
              'AD uuu23BC jjjEG iiiFA vvvSS',
              'AD hhh25BC kkkEG pppFA aaaSS',
              'AD ttt26BC xxxFA rrrSS'
            ];
    

    This is another version, using the input record separator $/:

    use strict;
    use warnings;
    use autodie;
    
    my $file = "fac.txt";
    open my $fh, '<', $file;
    
    my @data;
    $/ = "\nSS";
    while (<$fh>) {
        s/\n//g;
        push @data, $_;
    }
    
    use Data::Dumper;
    print Dumper \@data;
    

    Produces the same output with this data. It does not care about the record start characters, only the end, which is SS at the beginning of a line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not able to figure out why this does happen. On all non-webkit
I have encountered this problem a few times, and am not able to figure
I have a strange problem that I could not solve. When I try to
I have a problem that I have not faced before: It seems that the
I faced a problem that I can not find a way to change the
Problem: to show that a Not-SO-page has been discussed in SO when you at
I have a problem that someone introduced a bug but I do not know
I've got the problem that the UIAlertViewDelegate method - (void)alertViewCancel:(UIAlertView *)alertView is not called
I have a problem that happens in IE 8 and Firefox 6.0 but not
I have working registration script the only problem is that i do not know

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.