Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: to copy certain content in file & repeat it .

by wind (Priest)
on Jun 30, 2011 at 05:58 UTC ( #912097=note: print w/replies, xml ) Need Help??

in reply to to copy certain content in file & repeat it .

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/;

Replies are listed 'Best First'.
Re^2: to copy certain content in file & repeat it .
by harshmane (Initiate) on Jun 30, 2011 at 09:17 UTC
    #! /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]; } }


      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


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

      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.)

      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

        i m rewriting the current code here i m printing the give n part but i want it in between.
        use warnings; use strict; my $fg=0; my $i=0; open(FF ,"lib"); open (FF1,>lib1"); while(<FF>) { my $lin =$_; print FF1 $lin; if ($lin=~m/pattern to be patched for stat copying/) { $fg=1; } if ($fg) { $lin=$_; my @arr =$lin[$i]; $i++; } if($lin=~m/pattern where to stop copying & repeating/) { $fg=0; } } if ($fg==0) { for (my $j=0;$j<$i;$j++) { print FF1 @arr[$j]; } }
        i want to actuaaly print the given para in between here using these script i am print @ end of file.
      FROM next i would take care of it.

        FROM next i would take care of it.

        Hi, its me again :)

        I'm assuming this message was meant as a reply to Re^3: to copy certain content in file & repeat it .

        Since PerlMonks offers threaded discussions, it is important to reply to the correct node by clicking the [reply] alongside the node of interest

        This is something else to remember for next time

        Thanks :)

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2022-12-03 22:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found