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

Related Questions

I'm trying to read specific values from the text file (below): Current Online Users:
I'm trying to read a value from a file and use it in a
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
I am trying to read an XML-file from another server. However the the company
Are there any special considerations trying to read up data from an HTML form
I'm trying to read some BigDecimal values from the string. Let's say I have
I'm currently trying to read in an XML file, make some minor changes (alter
I'm trying to read a .doc file into a database so that I can
I am trying to read ASCII text response from a tcp open streaming socket
I'm trying to read a file to produce a DOM Document, but the file

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.