2

I have some Problem about the Splitting into an Array.

I want to split an empty line and save to an Array.

1.) First I read the File and save to a String ($configdata).

2.) Then I want split the String ($configdata) with the empty line.

My Script:

#!/usr/bin/perl

use strict;

my $pathconfigfile = 'config.conf';
my @configline;

open(my $configfile, "<", $pathconfigfile);

        while(<$configfile>){


                my $configdata = $_;


                my @configdata = split /\n\n/, $configdata;
                print @configdata[0] "\n";
                print @configdata[1] "\n";
                print @configdata[2] "\n";

        }
close $configfile;

Configfile:

Testingtttttttttttttttttttttttt
############################################
0987654345678909876MN09uz6t56789oiuhgölkjhgfr
0987654323456789098765fgnloiuztlkjhgfrtzuiknb

MegaMixoiuzt
############################################
09876543457890098765NSUDlkjhzgtfr67899ztz9098
098765435678987t87656789876567898765679097658

TESTINGPARTS
############################################
0987654567890098765hzzasza654567uhgdjdjfacdaa
9876545678987654mchfuiaq754567898765434567876

My Wish Result:

print @configdata[0];

Testingtttttttttttttttttttttttt
############################################
0987654345678909876MN09uz6t56789oiuhgölkjhgfr
0987654323456789098765fgnloiuztlkjhgfrtzuiknb

print @configdata[1];

MegaMixoiuzt
############################################
09876543457890098765NSUDlkjhzgtfr67899ztz9098
098765435678987t87656789876567898765679097658

print @configdata[2];

TESTINGPARTS
############################################
0987654567890098765hzzasza654567uhgdjdjfacdaa
9876545678987654mchfuiaq754567898765434567876

3 Answers 3

3

This is easier than you think, if you use $/ - the record separator.

E.g.:

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

use Data::Dumper;

local $/ = "\n\n"; 
#chomp removes $/ from the field)
chomp ( my @configdata = <DATA> );

print Dumper \@configdata


__DATA__
Testingtttttttttttttttttttttttt
############################################
0987654345678909876MN09uz6t56789oiuhgölkjhgfr
0987654323456789098765fgnloiuztlkjhgfrtzuiknb

MegaMixoiuzt
############################################
09876543457890098765NSUDlkjhzgtfr67899ztz9098
098765435678987t87656789876567898765679097658

TESTINGPARTS
############################################
0987654567890098765hzzasza654567uhgdjdjfacdaa
9876545678987654mchfuiaq754567898765434567876

Gives:

$VAR1 = [
          'Testingtttttttttttttttttttttttt
############################################
0987654345678909876MN09uz6t56789oiuhgölkjhgfr
0987654323456789098765fgnloiuztlkjhgfrtzuiknb',
          'MegaMixoiuzt
############################################
09876543457890098765NSUDlkjhzgtfr67899ztz9098
098765435678987t87656789876567898765679097658',
          'TESTINGPARTS
############################################
0987654567890098765hzzasza654567uhgdjdjfacdaa
9876545678987654mchfuiaq754567898765434567876'
        ];

Alternatively, you could get cute with map to make an array of arrays:

chomp ( my @configdata = map { [split] } <DATA> );

Which will give you:

$VAR1 = [
          [
            'Testingtttttttttttttttttttttttt',
            '############################################',
            '0987654345678909876MN09uz6t56789oiuhgölkjhgfr',
            '0987654323456789098765fgnloiuztlkjhgfrtzuiknb'
          ],
          [
            'MegaMixoiuzt',
            '############################################',
            '09876543457890098765NSUDlkjhzgtfr67899ztz9098',
            '098765435678987t87656789876567898765679097658'
          ],
          [
            'TESTINGPARTS',
            '############################################',
            '0987654567890098765hzzasza654567uhgdjdjfacdaa',
            '9876545678987654mchfuiaq754567898765434567876'
          ]
        ];

E.g.

$configdata[0][0] = 'Testingtttttttttttttttttttttttt'

Note - I'm using the inline __DATA__ filehandle for illustrative purposes. You would use your opened filehandle. ( chomp ( my @configdata = <$configfile> ); )

Also - $/ applies to while loops, so each iteration of your while would be the chunk of text you're intending to operate on - so you could instead:

while ( <DATA> ) { 
    chomp;
    print "Start of chunk:\n";
    print;
    print "\nEnd of chunk\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

$/ - wow, I've been using Perl for years and never come across this (nor the need to split files into records) but very useful.
local $/ = ""; (paragraph mode) would make far more sense.
1

Use input record separator

open(my $configfile, "<", $pathconfigfile) or die "$!";
local $/;
my @configdata  = split("\n\n",<$configfile>);
print @configdata;

2 Comments

@Skydreampower If my answer is useful for you, click the tick button for accepted answer :)
I'm not sure if that really counts as 'using' the record separator, as you're treating the file as a single record, then splitting it.
0
#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use TrigMod;

use Math::Trig;
use Math::Round;


print 
    'MENU
    1.  Calculate a if b and h are given.
    2.  Calculate h if a and b are given.
    3.  Calculate h if Y and a are given.
    4.  Calculate Y if h and a are given.
    ';
print "\nEnter Option";
my$opt=<>;


my$a, my$b, my$h, my$Y;


if($opt==1)
{
    print "\nEnter base and height b,h:";
    $b=<>;
    $h=<>;
    # b=6, h=4, a=5
    $a=TrigMod::geta($b,$h);
    $Y=TrigMod::getY($a,$h);
    print "\na=$a\theight=$h\tbase=$b\tangle=$Y";

}

elsif($opt==2)
{
    print "\nEnter sides a and b:";
    $a=<>;
    $b=<>;

    $h=TrigMod::geth_ab($a,$b);
    #a=5, b=6, h=4;
    print "\n$h";
    $Y=TrigMod::getY_ah($a,$h);
    print "\na=$a\tbase=$b\theight=$h\tangle=$Y";   
}

elsif($opt==3)
{
    print "Enter sides a and Y in degrees:";
    $a=<>;
    $Y=<>;

    $h=TrigMod::geth_aY($a,$Y);
    $b=TrigMod::getb_ah($a,$h);
    print "\na=$a\tbase=$b\theight=$h\tangle=$Y";   
}

elsif($opt==4)
{
    print "Enter sides a and h:";
    $a=<>;
    $h=<>;

    $Y=TrigMod::getY_ah($a,$h);
    $b=TrigMod::getb_ah($a,$h);
    print "\na=$a\tbase=$b\theight=$h\tangle=$Y";   
}

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.