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

perl substitue till found pattern in aline

by Vijay81 (Acolyte)
on Dec 02, 2012 at 18:17 UTC ( #1006740=perlquestion: print w/ replies, xml ) Need Help??
Vijay81 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, Am trying to substitute a comma in place of space in a line but till find the pattern. First part able to do but don't know how to do the part 2. Any help please

Script reads files(*.bat) from a perticular directory and read the content and substutite space with comma. bat file will have the pattern "-1". now i need subsutitue comma till find this pattern

bat file content :
01 fines name 2222 -P sws -1 reee.tee rrt 02 fi si 2232 -P sqww -1 re.wqw ttf
Main script content :
#!/usr/bin/perl use warnings; use strict; use Class::CSV; my ($csv, $inputfilename, $resultfile, $fh, $line, $dir, $fp, $count); $dir = 'C:\TestData'; open $resultfile, '>>', 'input.csv' or die "Can't open file: $!"; foreach $fp (glob("$dir/*.bat")) { #printf "%s\n", $fp; open ($fh, "<", $fp) or die "can't read open '$fp':"; while ($line = <$fh>) { #$line=~s/-l//; $line=~ s/ /,/g; print $resultfile $line; print $resultfile "\n"; print $line; } } close $fh or die "can't read close '$fp':";
output file should look like :
01,fines,name,2222,-P,sws -1 reee.tee rrt 02,fi,si,2232,-P,sqww,-1 re.wqw ttf

Comment on perl substitue till found pattern in aline
Select or Download Code
Re: perl substitue till found pattern in aline
by rjt (Deacon) on Dec 02, 2012 at 18:33 UTC
    while (<DATA>) { s/ (?=.*\s?-1 )/,/g; print; } __DATA__ 01 fines name 2222 -P sws -1 reee.tee rrt 02 fi si 2232 -P sqww -1 re.wqw ttf 02 fi si-1 2232 -P sqww -1 re.wqw ttf

    This will replace spaces with commas before the last -1 in the file.

      This variant is a little bit more easy to read and should be executed faster.

      while (<DATA>) { my ( $left, $right ) = split /-1/, $_, 2; $left =~ s/ /,/g; print join '-1', $left, $right; } __DATA__ 01 fines name 2222 -P sws -1 reee.tee rrt 02 fi si 2232 -P sqww -1 re.wqw ttf 02 fi si-1 2232 -P sqww -1 re.wqw ttf

        Hi!

        Your code is slower by about a factor of 3:

        use Benchmark qw/:all :hireswallclock/; my $DATA = '01 fines name 2222 -P sws -1 reee.tee rrt'; cmpthese(-10, { 'rjt' => q{ $_ = $DATA; s/ (?=.*\s?-1 )/,/g; }, 'pushtaev' => q{ $_ = $DATA; my ( $left, $right ) = split /-1/, $_, 2; $left =~ s/ /,/g; join '-1', $left, $right; }, });
        Rate pushtaev rjt pushtaev 1875006/s -- -75% rjt 7582716/s 304% --

        Regarding readability, it will depend who you ask in this case. In my experience, I tend to find a simple regex easier to read than tearing apart and piecing a solution back together with split, s///, and join.

        Lastly, in your code, replacing this:

            print join '-1', $left, $right;

        ... with this:

            print $left, '-1', $right;

        ... resulted in a 32% speed improvement before I/O on your solution (still ~2.2x slower than the straight regex) and puts the terms in their natural order, which is probably more readable than the strange use of join with a delimiter to concatenate three strings.

        I hope this is useful feedback to your feedback!

Re: perl substitue till found pattern in aline
by johngg (Abbot) on Dec 02, 2012 at 19:24 UTC

    Your example output seems to be inconsistent. In the first line the space before the '-1' is not substituted with a comma but in the second it is. Please could you clarify your requirement.

    Cheers,

    JohnGG

      Sorry it was typo mistake
      01,fines,name,2222,-P,sws,-1 reee.tee rrt 02,fi,si,2232,-P,sqww,-1 re.wqw ttf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-07-30 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls