Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: how to split file with some pattern

by blindluke (Hermit)
on Jan 23, 2015 at 08:21 UTC ( [id://1114243]=note: print w/replies, xml ) Need Help??


in reply to how to split file with some pattern

It's a shame that you did not tell us what you already have tried, and what went wrong with your attempt. You are also missing some important information: how are the output files supposed to look? Should they contain the delimiter lines? Or should they contain everything, including module and endmodule?

Since the text blocks seem not to contain any blank lines, here is a very lazy way to solve the problem you are having.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $file = 'soc.v'; open(my $fh, "<", $file) or die "Cannot open the file!"; my @data; $/ = ""; while (my $paragraph = <$fh>) { $paragraph =~ m/^\s*module\s*([A-Za-z]+)/; push @data, { filename => $1, content => $paragraph }; } #use Data::Dumper; print Dumper \@data; for my $outfile (@data) { open(my $out, ">", $outfile->{filename}) or die "Cannot open the f +ile!"; print $out $outfile->{content}; }

This solution uses the "paragraph mode" - this line: $/ = ""; makes sure that the iterating over the contents of the file is done not by line, as it usually is, but by whole paragraphs. If you uncomment the Data::Dumper line, you will see that the data from your input file is parsed into an array of hash references, in a way that gives you the information about the contents of the output file and its name.

Good luck with your problem.

- Luke

Replies are listed 'Best First'.
Re^2: how to split file with some pattern
by herman4016 (Acolyte) on Jan 23, 2015 at 10:16 UTC

    1. sorry , I should say it more detail. if a blank line in module/endmodule the program you write will stop to run , that is not what I want. please ignore the blank line and let the program continue run until see "endmodule"

    2. yes , I hope text in module/endmodule to a new file include module/endmodule.

    Thanks !!

      Good. Now that you have your requirements figured out, try to combine elements of my solution with the suggestions provided by this reply by Discipulus. In particular, ignore the "paragraph mode", start with the while loop suggested by Discipulus, expand the pattern he provided by capturing the desired file name (look at this regexp in my reply: m/^\s*module\s*([A-Za-z]+)/), and finish with writing to files, using either the approach I have show or the one suggested by vinoth.ree.

      Try writing your script, and if you encounter any problems, post your code and we will do our best to help you improve it.

      - Luke

      you have the solution in my first reply: you just need to fire @files elemnts in separate files..
      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1114243]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2025-12-08 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (87 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.