Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

to copy certain content in file & repeat it .

by harshmane (Initiate)
on Jun 30, 2011 at 04:56 UTC ( #912090=perlquestion: print w/ replies, xml ) Need Help??
harshmane has asked for the wisdom of the Perl Monks concerning the following question:

the work is like in a give .txt file i have to find a given word & parargraph s following that shud be repeated as it is till any other specific wor or character is not encounterd . for eg.
hero{ #starting word ........ #all the lines shud be copied. ....... ....... ...... } #till diz point. copy_hero{ {......... .......... .......... }

Comment on to copy certain content in file & repeat it .
Download Code
Re: to copy certain content in file & repeat it .
by wind (Priest) on Jun 30, 2011 at 05:58 UTC

    Dude.... that is like the absolutely worst explanation ever. Did you proof read it at all?

    Where's your code? What have you tried thus far? I know what I mean. Why don't you?

    If all you care about is doubling one section of content then you could just read the entire file into a variable and then use a regex. The biggest challenge is determining what the regex should match, and we can't tell you that given the limitted information that you've provided.

    $contents =~ s/(match me)/$1$1/;
      #! /usr/bin/perl use warnings; open (FF,"src.txt"); # fro where i have to read. open (FF1,">dest.txt"); # while (<FF>) { $lin=$_; print FF1 $lin; if (/input/) # input i a word after dat all paragraph shud be copied + till other wor is not encounterd diz word is #repeated is present tw +ice in given file {$i=0 while(<FF>) { @arr =$_; print FF1 @arr; $i++; exit if ($arr =~/term/); } } } while (<FF>) { for ($j=0;$j<$i;$j++) { print FF1 @arr[$j]; } }
      #! /usr/bin/perl use warnings; open (FF,"src.txt"); # fro where i have to read. open (FF1,">dest.txt"); # while (<FF>) { $lin=$_; print FF1 $lin; if (/input/) # input i a word after dat all paragraph shud be copied + till other wor is not encounterd diz word is #repeated is present tw +ice in given file {$i=0 while(<FF>) { @arr =$_; print FF1 @arr; $i++; exit if ($arr =~/term/); } } } while (<FF>) { for ($j=0;$j<$i;$j++) { print FF1 @arr[$j]; } }
        There is no need to create duplicates. As Writeup Formatting Tips tells you

        The three most important words in posting

        Preview, Preview, Preview. Even though, in most cases, you can edit your node after it has been submitted, some sections of the site do not allow this, so be sure that everything is looking how you want others to see it before you finally hit Submit. (In your User Settings you can switch off the forced preview. But don't. Even the most experienced monks still preview before posting. Your humble author has previewed up to eight times on some posts, just to make sure it would look right.)
        FROM next i would take care of it.

        harshmane:

        I'd suggest first reindenting your program so the structure is clear (use perltidy or the indenting feature of a [no such wiki, comparison of text editors] such as vim, emacs, etc).

        Next, for a program as small as this one, pretend you're the computer, and simply do the operations one by one. If you're moderately careful, you'll find the bugs pretty easily as you'll find your program telling you to do things at the wrong time. Just take a sheet of paper and write a small simple data set on it at the top. Use the bottom to keep track of the values of variables and the output file. Then, as you find the bugs, repair them.

        While I find desk work like that a bit relaxing, you'll find that more complex programs require far too much work to do by hand. That's when using the perl debugger comes in handy. Learn how to use it, and it will help you become a better programmer.

        But for this program, it would be instructive if you do it by hand so you can get a feel for how to read a program and find errors. As an example, for the program:

        my $total=0; for my $val (2 4 6 8) { $total += $val; } print $total;

        My worksheet would look something like:

        total: 0 2 6 12 20

        val: 2 4 6 8

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        Hi, the code you posted does not compile, part of asking a question effectively, is to make sure your code compiles before posting.

         

        while(<FH>){ ...

        This reads from a file until it reaches the end. Filehandles are iterators. Once you reach the end, the subsequent while(<FH>) loop will not be entered

         

        @arr =$_;

        This sets @arr to the value of $_; It doesn't add values to @arr, for that you need the push function

Re: to copy certain content in file & repeat it .
by davido (Archbishop) on Jun 30, 2011 at 06:23 UTC

    perl -pi.bak -07d -e 's/(hero\{.*)/$1 . 'copy_' . $1/es;' filename.txt

    Untested. It should set the input record separator to the '}' character, and then finding the start tag it subs everything between start and end tags with a second copy, and makes a backup just in case I got it entirely wrong. ;)

    The /e isn't strictly necessary, but it allowed me to make the right hand side of the s/// operator easier to read.


    Dave

Re: to copy certain content in file & repeat it .
by Anonymous Monk on Jun 30, 2011 at 06:29 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-09-21 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls