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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:23:11+00:00 2026-05-12T00:23:11+00:00

I am trying to read values from an input file in Perl. Input file

  • 0

I am trying to read values from an input file in Perl.
Input file looks like:

1-sampledata1 This is a sample test
              and data for this continues
2-sampledata2 This is sample test 2
              Data for this also is on second line

I want to read the above data so that data for 1-sampledata1 goes into @array1 and data for 2-sampledata2 goes in @array2 and so on.
I will have about 50 sections like this. like 50-sampledata50.

EDIT: The names wont always be X-sampledataX. I just did that for example. So names cant be in a loop. I think I’ll have to type them manually

I so far have the following (which works). But I am looking for a more efficient way to do this..

foreach my $line(@body){
        if ($line=~ /^1-sampledata1\s/){
                $line=~ s/1-ENST0000//g;
                $line=~ s/\s+//g;
                push (@array1, $line);
          #using splitarray because i want to store data as one character each
          #for ex: i wana store 'This' as T H I S in different elements of array
                @splitarray1= split ('',$line);
        last if ($line=~ /2-sampledata2/);
        }
}
foreach my $line(@body){
        if ($line=~ /^2-sampledata2\s/){
                $line=~ s/2-ENSBTAP0//g;
                $line=~ s/\s+//g;
                @splitarray2= split ('',$line);
        last if ($line=~ /3-sampledata3/);
        }
}

As you can see I have different arrays for each section and different for loops for each section. If I go with approach I have so far then I will end up with 50 for loops and 50 arrays.

Is there another better way to do this? In the end I do want to end up with 50 arrays but do not want to write 50 for loops. And since I will be looping through the 50 arrays later on in the program, maybe store them in an array? I am new to Perl so its kinda overwhelming …

  • 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-12T00:23:11+00:00Added an answer on May 12, 2026 at 12:23 am

    The first thing to notice is that you are trying to use variable names with integer suffixes: Don’t. Use an array whenever you find your self wanting to do that. Second, you only need to read to go over the file contents once, not multiple times. Third, there is usually no good reason in Perl to treat a string as an array of characters.

    Update: This version of the code uses existence of leading spaces to decide what to do. I am leaving the previous version up as well for reference.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my @data;
    
    while ( my $line = <DATA> ) {
        chomp $line;
        if ( $line =~ s/^ +/ / ) {
            push @{ $data[-1] }, split //, $line;
        }
        else {
            push @data, [ split //, $line ];
        }
    }
    
    use Data::Dumper;
    print Dumper \@data;
    
    __DATA__
    1-sampledata1 This is a sample test
                  and data for this continues
    2-sampledata2 This is sample test 2
                  Data for this also is on second line
    

    Previous version:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my @data;
    
    while ( my $line = <DATA> ) {
        chomp $line;
        $line =~ s/\s+/ /g;
        if ( $line =~ /^[0-9]+-/ ) {
            push @data, [ split //, $line ];
        }
        else {
            push @{ $data[-1] }, split //, $line;
        }
    }
    
    use Data::Dumper;
    print Dumper \@data;
    
    __DATA__
    1-sampledata1 This is a sample test
                  and data for this continues
    2-sampledata2 This is sample test 2
                  Data for this also is on second line
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 161k
  • Answers 161k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Hmmm... Putting the favicon in the root works in Firefox… May 12, 2026 at 11:48 am
  • Editorial Team
    Editorial Team added an answer You can add a custom subview to your UIView that… May 12, 2026 at 11:48 am
  • Editorial Team
    Editorial Team added an answer See Apple Developers thread on NSDateComponents and weekday NSCalendar *gregorian… May 12, 2026 at 11:48 am

Related Questions

I'm trying to input data from an XML file in a C++ program using
I have looked for a good example of a Builder pattern (in C#), but
(Edited to add an example and hopefully make it a bit clearer) I'm mainly
I have a new laptop at work and code that worked earlier in the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.