Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

needed help

by zee3b (Novice)
on Feb 12, 2013 at 19:17 UTC ( #1018430=perlquestion: print w/ replies, xml ) Need Help??
zee3b has asked for the wisdom of the Perl Monks concerning the following question:

hey guys i needed some help

Comment on needed help
Re: Perl Sorting Help
by toolic (Chancellor) on Feb 12, 2013 at 19:59 UTC
    That looks like a highly-structured input file. Do you know if it is some standard format? If so, you should first search CPAN to see if someone else has already created a parser for it.
Re: Perl Sorting Help
by frozenwithjoy (Curate) on Feb 12, 2013 at 21:01 UTC
    As toolic said, you should use a module for this if there is one available. However, if not, I threw this together. It works for this set, but you may need to customize it depending on how (ir)regular your input is:
    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; say "Code:"; my @current_command; while (<DATA>) { chomp; push @current_command, $1 if /^\s*(.+)\s{$/; if (/^\s*(.+);$/) { my $last_term = $1; say "set @current_command $last_term"; } pop @current_command if /}$/; } __DATA__ deviceconfig { system { snmp-setting { snmp-system { location "VA"; contact US; send-event-specific-traps yes; } access-setting { version { ll1 { snmp-community-string trap; } } } }

    Output:

    Code: 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-t +raps yes set deviceconfig system snmp-setting access-setting version ll1 snmp-c +ommunity-string trap
      Thanks a lot for the reply. Now what if I was using a txt file as an input. Where would the fine name go. I'm sorry for asking such silly questions as I am a beginner and trying really hard to understand.
        Take a look at the first example in open. Once you open your file handle, replace <DATA> with <$your_filehandle> at the beginning of the loop.
        Hey zee3b, I figured I'd respond to your PM here so I'd have more room.
        Hey, thanks a lot for the help. That really worked and was exactly what I was looking for. When you get a few mins, can you please explain how the code works for my understanding. Thanks!
        # Read through file/data one line at a time. while (<DATA>) { # Remove newline from end of line (actually not necessary for your + input). chomp; # If a line ends with '{', use a regular expression (REGEX) to cap +ture # the relevant portion by having that portion surrounded by parent +heses. # Push the captured string (contained within the variable '$1', si +nce it # is the first captured string) onto the end of the array '@curren +t command'. # This REGEX matches: # the beginning of the line: ^ # 0 or more spaces: \s* # 1 or more of anything (captured because it is in parentheses +): (.+) # 1 space followed by 1 {: \s{ # the end of the line: $ push @current_command, $1 if /^\s*(.+)\s{$/; # If a line ends with ';', use a REGEX to capture the relevant por +tion, # then print out the contents of the array followed by the string +you # just captured and assigned to '$last_term'. if (/^\s*(.+);$/) { my $last_term = $1; say "set @current_command $last_term"; } # If a line ends with '}', that means we are moving up a level, so + you # remove the last element of the array '@current_command'. pop @current_command if /}$/; }
Re: Perl Sorting Help
by 7stud (Deacon) on Feb 12, 2013 at 23:47 UTC

    A quicker way for you to learn perl would be to read "Learning Perl 6th ed.", and do the exercises at the end of each chapter. Depending on your computer programming experience that could take less than a week. After that, you'll need to learn about references, so you would need to read at least the beginning of "Intermediate Perl".

    Regular expressions are another whole language unto themselves, and most computer programming languages employ them, so you'll have to start slow and gain experience as you go along. Regular expressions can be very frustrating when you are just starting out--make sure you understand the 'default greediness' of regexes and that will help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018430]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-10-20 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (93 votes), past polls