Perl Sorting Help

Hey guys,
I have started to learn perl recently because of a position I took. They want me to master perl and I've been reading books and practicing myself.
Basically I,m having my perl script run through a text pad and give the output in a special way

e.g

input 

deviceconfig {
  system {
    snmp-setting {
      snmp-system {
        location "VA";
        contact US;
        send-event-specific-traps yes;
      }
      access-setting {
        version {
          ll1 {
            snmp-community-string trap;
          }
        }
      }
    }

the output should be like this

set deviceconfig system snmp-setting snmp-system location "VA"
set deviceconfig system snmp-setting snmp-system contact US
set deviceconfig system snmp-setting snmp-system send-event-specific-traps yes
set deviceconfig system snmp-setting access-setting version ll1 snmp-community-string trap

so far I have

open (NAMES_FILE, "<input.txt")  or  die "Failed to read file : $! ";
my @not_sorted = <NAMES_FILE>;  # read entire file in the array
print @not_sorted;
close (NAMES_FILE);

so I am putting all the file into an array, but what I want to do is, I want it to paste everything before a bracket open and the statement after it. I would really appreciate some help.

You may either do this using vanilla Perl or make your life very simple by using a configuration file parser such as Config::Scoped. See CPAN for its documentation. Also check out other config parsers (although this one will do your job).

Since I am accessing the forum through my mobile, can't help you much. Sorry.

1 Like

Hey, thanks a lot for the reply. I tried using Config::Scope and I just can't figure out how to set it so that it still comes in that format.

Im sorry, but i am not quite familiar with perl and am just a beginner.

Without using any config parsers and with certain assumptions about your input data (based on the sample provided):

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

open my($in_file), '<', '/path/to/inputfile'
or die "Could not open file: $!\n";

my @params = qw(set);

while(<$in_file>) {
 next if (/^\s+$|^\s*#/);
 if(/^\s*}\s*$/) { pop @params; next}
 my @tokens = split;
 if ($tokens[1] =~ /{/) { push @params, $tokens[0]; next }
 s/^[ \t]*|;.*$//g;
 print "@params $_";
}

close $in_file;
1 Like

Hey, Thanks a lot. That really did help. I'm currently in the process of learning perl and am putting all the effort I can.

Once again, Thank you!

my @arr;
while(<DATA>){
	chomp;
  if(/\{/){
	s/[ \{]//g;
	push @arr, $_;
  }
  elsif(/\}/){
	pop @arr;
  }
  else{
	my $pre = join(", ",@arr);
	print $pre, $_,"\n";
  }
}
__DATA__
deviceconfig {
  system {
    snmp-setting {
      snmp-system {
        location "VA";
        contact US;
        send-event-specific-traps yes;
      }
      access-setting {
        version {
          ll1 {
            snmp-community-string trap;
          }
        }
      }
    }