Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Problem changing only first occurence of pattern in file

by JimJx (Beadle)
on Oct 17, 2006 at 13:18 UTC ( [id://578755]=perlquestion: print w/replies, xml ) Need Help??

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

I have a small flatfile, ~100 lines, that I need to access and change one entry in. For example

abc1|0001|Yes
abc2|0002|Yes
abc3|0003|No
abc4|0004|No
....
abc100|0100|No

What I need to do is find the first occurance of No and change it to Yes. Pretty simple usually.... But for some reason, it will find the first occurance and change that and the next occurance also.

What I get is.
abc1|0001|Yes
abc2|0002|Yes
abc3|0003|Yes
abc4|0004|Yes

Any suggestions?
Thanks!
Jim

open INF,'</var/www/htdocs/cgi-bin/user.txt' or die "Can't open input +file"; my $flag = 0; while (<INF>) { local($^I, @ARGV) = ('.bak', 'user.txt'); while (<>) { if (/^(.+)(\|No)$/ && $flag != 1) { $flag = 1; print "$1|Yes\n"; ($Name,$Pass,$Used) = split /\|/,$_,3; } else { print; } } } close INF;

2006-10-17 Retitled by jeffa, as per Monastery guidelines
Original title: 'Driving me crazy.....'

Replies are listed 'Best First'.
Re: Problem changing only first occurence of pattern in file
by japhy (Canon) on Oct 17, 2006 at 13:25 UTC
    You're looping more than you mean to. I think you want to pare your code down to just:
    my $flag = 0; local ($^I, @ARGV) = (".bak", "/var/ww/htdocs/cgi-bin/user.txt"); while (<>) { $flag = 1 if !$flag and s/\|No$/|Yes/; print; }

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Problem changing only first occurrance of pattern in file
by davorg (Chancellor) on Oct 17, 2006 at 13:26 UTC

    Please give your questions meaningful titles.

    You're probably better off using Tie::File for something like this.

    use Tie::File; tie my @file, 'Tie::File', 'user.txt'; for (@file) { s/\|No$/|Yes/ and last; }
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Don't forget to make the back-up file and also verify that the file was opened correctly:   :-)
      use Tie::File; use File::Copy; my $file = 'user.txt'; copy $file, "$file.bak" or die "Cannot copy '$file' $!"; tie my @file, 'Tie::File', $file or die "Cannot open '$file' $!"; for ( @file ) { s/\|No$/|Yes/ and last; }
Re: Problem changing only first occurence of pattern in file
by cephas (Pilgrim) on Oct 17, 2006 at 13:33 UTC
    perl -ne 'if(s/No/Yes/){ print; last; } else {print} }{print while(<>)' user.txt

    This will loop, through printing lines, and change the first one that has a No, and then just finish looping printing the lines as is.
Re: Problem changing only first occurence of pattern in file
by nimdokk (Vicar) on Oct 17, 2006 at 13:27 UTC
    I could be wrong, but what I think you need to do is break out of the loops with a last statement. What I'd try would be to name the code blocks (i.e. FIRST and SECOND) then after your split, do last FIRST;. I think that will break you out of the loops. This is untested and may not work quite the way I described it.
Re: Problem changing only first occurence of pattern in file
by jdporter (Paladin) on Oct 17, 2006 at 15:12 UTC
    perl -pe "/yes$/../no$/ and s/no$/yes/"
    We're building the house of the future together.
Re: Problem changing only first occurence of pattern in file
by Hue-Bond (Priest) on Oct 17, 2006 at 13:27 UTC
Re: Problem changing only first occurence of pattern in file
by jwkrahn (Abbot) on Oct 17, 2006 at 14:40 UTC
    local( $^I, @ARGV ) = ( '.bak', '/var/www/htdocs/cgi-bin/user.txt' ); ?\|No$? && s/No$/Yes/ while <>;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-19 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found