TheBigAmbulance has asked for the wisdom of the Perl Monks concerning the following question:

Hey All,

This will probably be a no-brainer for you peoples here, but I have a quick question.

Let's say I have a text file with the following lines.

test
test
test
test
test
test

I have figured out how to get this to work for 2 entries.  Just like this:
perl -pe 's/test/$state = not $state; $state ? "test1" : "test2"/eg;$_'

This would give me:
test1
test2
test1
test2

What I need is a way to do this more dynamically.  Let's say I wanted 5 responses instead of two.  The output would be like this:

test1
test2
test3
test4
test5
test1
etc...

Could anyone give me a couple of pointers on how to do this?  I haven't used perl very much.  But am finding a need to learn some scripting.

Thanks!
  • Comment on search and replace with serialized answers

Replies are listed 'Best First'.
Re: search and replace with serialized answers
by VinsWorldcom (Prior) on May 01, 2009 at 19:08 UTC
    #!/usr/bin/perl use strict; open (INFILE, "$ARGV[0]"); my @lines = <INFILE>; close (INFILE); my $responses = 0; foreach (@lines) { if ($responses < $ARGV[1]) { $responses++ } else { $responses = 1; } chomp $lines[$_]; print $lines[$_] . $responses . "\n" }

    input.txt

    test test test test test test test test test test test

    Called as:

    {C} > test input.txt test1 test1 test1 test1 test1 test1 test1 test1 test1 test1 test1 {C} > test input.txt 5 test1 test2 test3 test4 test5 test1 test2 test3 test4 test5 test1
      That's great.  I'm on the right path.  Let me throw in another curve.

      Let's say my input file is as follows:

      jack test is a good boy
      jill test is a good boy
      john test is a good boy
      mary test is a good boy
      paul test is a good boy
      link test is a good boy
      karl test is a good boy

      Using a simliar script, how would i have it sniff out the text "test" and then serialize the response per the same type of scheme inputing the number of times I want to serialize it?  Let's say I want to use 5 times so the results would be:

      jack test1 is a good boy
      jill test2 is a good boy
      john test3 is a good boy
      mary test4 is a good boy
      paul test5 is a good boy
      link test1 is a good boy
      karl test2 is a good boy

        and what have you tried in order to reach your goal ? please show us so we can direct you on 'the right path'. have you tried modifying the example VinsWorldcom gave ? what was your conclusion and what difficulty did you encounter ?

        I think you should get a Perl book(maybe this one) and try to read it

Re: search and replace with serialized answers
by jwkrahn (Abbot) on May 01, 2009 at 19:33 UTC
    perl -pe's/(?<=test)/ $state++ % 5 + 1 /e'
      Thanks to those that helped me answer this poser. I'm not very fluent in perl, and just wanted to ask for some advice from people who know more about it than me. Thank You Very Much!
Re: search and replace with serialized answers
by MidLifeXis (Monsignor) on May 01, 2009 at 19:37 UTC

    Homework?

    --MidLifeXis

    The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post

Re: search and replace with serialized answers
by bichonfrise74 (Vicar) on May 02, 2009 at 01:11 UTC
    How about this...
    #!/usr/bin/perl use strict; my $max = 3; my $counter = 1; while (<DATA>) { chomp; print "${_}$counter\n"; $counter == $max ? $counter = 1 : $counter++; } __DATA__ test test test test test test
Re: search and replace with serialized answers
by spx2 (Deacon) on May 01, 2009 at 19:33 UTC
    this seems to work pretty well for what you want to do cat bla | perl -pe '$a=++$a%5;$b=$a+1;s/\n/$b\n/'