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

  • SEARCH
  • Home
  • 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 8372115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:20:25+00:00 2026-06-09T14:20:25+00:00

I have written a Perl script: #!/usr/bin/perl use strict; use warnings; my $file_name; my

  • 0

I have written a Perl script:

#!/usr/bin/perl

use strict;
use warnings;

my $file_name;
my $ext = ".text";
my $subnetwork2;
my %files_list = ();
    opendir my $dir, "." or die "Cannot open directory: $!";
    my @files = readdir $dir;

sub create_files() {

    my $subnetwork;
    open(MYFILE, 'file.txt');
    while (<MYFILE>) {
        if (/.subnetwork/) {
            my @string = split /[:,\s]+/, $_;
            $subnetwork = $string[2];
        }
        if (/.set/ && (defined $subnetwork)) {
            my @string = split /[:,\s]+/, $_;
            my $file = $subnetwork . $string[1];
            open FILE, ">", "$file.text" or die $!;
            close(FILE);
        }
    }
    close(MYFILE);
}

sub create_hash() {
    foreach (@files) {
        if (/.text/) {

            open($files_list{$_}, ">>$_") || die("This file will not open!");

        }
    }
}

sub init() {
    open(MYFILE3, 'file.txt');
    while (<MYFILE3>) {
        if (/.subnetwork/) {
            my @string3 = split /[:,\s]+/, $_;
            $subnetwork2 = $string3[2];
            last;
        }
    }
    close(MYFILE3);
}

sub main_process() {

    init;
    create_files;
    create_hash;

    open(MYFILE1, 'file.txt');
    while (<MYFILE1>) {
        if (/.subnetwork/) {
            my @string3 = split /[:,\s]+/, $_;
            $subnetwork2 = $string3[2];
        }
        if (/.set/) {
            my @string2 = split /[:,\s]+/, $_;
            $file_name = $subnetwork2 . $string2[1] . $ext;
        }
        if (/.domain/ || /.end/ || ($. < 6)) {
            my $domain = $_;
            foreach (@files) {
                if (/.text/ && /$subnetwork2/) {
                    prnt { $files_list{$_} } "$domain";
                }
            }
        }
        elsif ($. >= 6) {
            print { $files_list{$file_name} } "$_";
        }
     }
    close(MYFILE1);
    foreach my $val (values %files_list) { close($val); }
    closedir $dir;
}

main_process;

This script creates files in the current directory based upon the content of file.txt, and then open those files again.

Then it starts processing file.txt and redirects the lines according to the filename set dynamically.

This setting of the file name is also based upon the data in the file file.txt.

The problem that I am facing here is that the redirection is only to a single file. That means there is some problem with the file handle.

All the files that are expected to be created are created perfectly but the data goes into only one of them.

I doubt if there is a problem with the file handle that I am using while redirecting.

Could anyone please help?
Sample input file is below:

..cnai #Generated on Thu Aug 02 18:33:18 2012 by CNAI R21D06_EC01, user tcssrpi
..capabilities BASIC
.utctime 2012-08-02 13:03:18
.subnetwork ONRM_ROOT_MO:NETSim_BAG
.domain BSC
.set BAG01
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
AMRCSFR3ICM=
AMRCSFR4ICM=
USERDATA=""
.set BAG02
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
..end

The problem that i am facing is during execution:

> process.pl 
Use of uninitialized value in ref-to-glob cast at process.pl line 79, <MYFILE1> line 6.
Can't use string ("") as a symbol ref while "strict refs" in use at process.pl line 79, <MYFILE1> line 6.

The problem i can understand is with this line:

print { $files_list{$_} } "$domain";

but i am unable to understand why!!

The output i need is :

> cat NETSim_BAGBAG01.text 
.set BAG01
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
AMRCSFR3ICM=
AMRCSFR4ICM=
USERDATA=""


> cat NETSim_BAGBAG02.text
.set BAG02
AFRVAMOS="OFF"
AWBVAMOS="OFF"
ALPHA=0
AMRCSFR3MODE=1,3,4,7
AMRCSFR3THR=12,21,21
AMRCSFR3HYST=2,3,3
> 
  • 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-06-09T14:20:26+00:00Added an answer on June 9, 2026 at 2:20 pm

    Your problem in following lines:

    open(PLOT,">>$_") || die("This file will not open!");
    $files_list{$_}=*PLOT;
    

    You should replace they with:

    open($files_list{$_},">>$_") || die("This file will not open!");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following Perl script- use HTML::TreeBuilder; my $html = HTML::TreeBuilder->new_from_content(<<END_HTML); <span
I have written a perl script to send email using the gmail's smtp server:
I have written a Perl script for the following bioinformatics question, but unfortunately there
I have written a Perl script, I just want to give it to every
Say I have a script written in perl or python. What's the easiest way
I have a script that's written in perl, and executed as CGI. It works
I have written a Perl script that runs as a daily crontab job that
I have written a Perl script that reads some data and generates an OpenOffice
I have written a simple perl script to read a line from a .csv
I have written a Perl script to read the configuration file and create CGI

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.