0

Text file contains

pChainCtrl 
pNewRoute 
pCurrRoute 

pChainCtrl 
pNewRoute 
pCurrRoute 
pMod

pChainCtrl 
pNewRoute 

Is it possible to put the set of elements into an array like

array a[0] = pChainCtrl 
             pNewRoute 
             pCurrRoute 

a[1] =      pChainCtrl 
            pNewRoute 
            pCurrRoute 
            pMod
0

2 Answers 2

2

Try this:

#!/usr/bin/perl -w

open my $fh, "data.txt" or die;
my $file = do { local $/; <$fh> };
close $fh;

my @a = split /\n{2,}/, $file;
Sign up to request clarification or add additional context in comments.

1 Comment

If you set $/ to an empty string, rather than undef then this all becomes much simpler - my @a = do { local $/ = ''; <$fh> };
2

You can change the input record separator $/ to "" (i.e., local $/ = "";) to split the input on empty lines (see perldoc perlvar for details).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.