Change alternate commas to semi colons...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • StevePBurgess@gmail.com

    #1

    Change alternate commas to semi colons...

    With a string of authors such as:

    Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
    Shawyer, Andrea

    I would like to write a function to change every other comma into a
    semi colon (thereby defining where one name ends and the next begins).

    I could do it by writing a complex (and slow) procedure - but is there
    a quick way of doing it using regular expressions, for example?

    Many thanks.

  • bgeneto

    #2
    Re: Change alternate commas to semi colons...


    StevePBurgess@g mail.com wrote:[color=blue]
    > With a string of authors such as:
    >
    > Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
    > Shawyer, Andrea
    >
    > I would like to write a function to change every other comma into a
    > semi colon (thereby defining where one name ends and the next begins).
    >
    > I could do it by writing a complex (and slow) procedure - but is there
    > a quick way of doing it using regular expressions, for example?
    >
    > Many thanks.[/color]

    Why not just use str_replace() intrinsic function?

    $str = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev,
    Karen, Shawyer, Andrea";
    $str = str_replace("," , ";", $str);
    echo $str;

    Bernhard Enders.

    Comment

    • Rik

      #3
      Re: Change alternate commas to semi colons...

      StevePBurgess@g mail.com wrote:[color=blue]
      > With a string of authors such as:
      >
      > Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
      > Shawyer, Andrea
      >
      > I would like to write a function to change every other comma into a
      > semi colon (thereby defining where one name ends and the next begins).
      >
      > I could do it by writing a complex (and slow) procedure - but is there
      > a quick way of doing it using regular expressions, for example?[/color]


      Regular expressions can be used, I'd think this is faster:

      $array = explode(',', $authors);
      $new_authors = '';
      $i = 0;
      while(!empty($a rray)){
      $delimter = (($i % 2)==0) ? ',' : ';';
      $new_authors .= array_shift($ar ray).$delimiter ;
      $i++;
      //or, to get an array:
      //$new_authors[] = array_shift($ar ray).','.array_ shift($array);
      }


      If you want a regular expression (which almost certainly will be slower):
      $authors = preg_replace('/([^,]*),([^,]*),/s','$1,$2;',$au thors);

      Similar, to get them directly in an array:
      preg_match_all( '/([^,]*,){2}/s',$authors,$ma tches);


      Grtz,
      --
      Rik Wasmus


      Comment

      • Rik

        #4
        Re: Change alternate commas to semi colons...

        bgeneto wrote:[color=blue]
        > Why not just use str_replace() intrinsic function[/color]
        [color=blue]
        > StevePBurgess@g mail.com wrote:[color=green]
        >> I would like to write a function to change every other comma into a[/color][/color]
        ----------------------------------------------------^^^^^

        Grtz,
        --
        Rik Wasmus


        Comment

        • Juliette

          #5
          Re: Change alternate commas to semi colons...

          bgeneto wrote:[color=blue]
          > StevePBurgess@g mail.com wrote:[color=green]
          >> With a string of authors such as:
          >>
          >> Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
          >> Shawyer, Andrea
          >>
          >> I would like to write a function to change every other comma into a
          >> semi colon (thereby defining where one name ends and the next begins).
          >>
          >> I could do it by writing a complex (and slow) procedure - but is there
          >> a quick way of doing it using regular expressions, for example?
          >>
          >> Many thanks.[/color]
          >
          > Why not just use str_replace() intrinsic function?
          >
          > $str = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev,
          > Karen, Shawyer, Andrea";
          > $str = str_replace("," , ";", $str);
          > echo $str;
          >
          > Bernhard Enders.
          >[/color]

          Because he wants to change every *other* comma into a semi-colon (i.e.
          change the second, fourth etc).
          Your function would change *every* comma into a semi-colon.

          I don't think you can easily do it with regex as you can't really
          differentiate between comma's.
          I would suggest something along the lines of:

          $array = explode ( ', ', $string );
          $arraycount = count($array);
          $new_string = '';

          for( $i=0; $i<$arraycount; $i++; ) {
          if ((1&$i)) {
          //$i is odd
          $new_string .= $array[$i] . ', ';
          }
          elseif (!(1&$num)) {
          //$i is even
          $new_string .= $array[$i] . '; ';
          }
          }
          $string = $new_string;
          unset($new_stri ng);

          Hope this helps,
          Juliette

          Comment

          • Ken Robinson

            #6
            Re: Change alternate commas to semi colons...

            Juliette wrote:
            [color=blue]
            > I don't think you can easily do it with regex as you can't really
            > differentiate between comma's.
            > I would suggest something along the lines of:
            >
            > $array = explode ( ', ', $string );
            > $arraycount = count($array);
            > $new_string = '';
            >
            > for( $i=0; $i<$arraycount; $i++; ) {
            > if ((1&$i)) {
            > //$i is odd
            > $new_string .= $array[$i] . ', ';
            > }
            > elseif (!(1&$num)) {
            > //$i is even
            > $new_string .= $array[$i] . '; ';
            > }
            > }
            > $string = $new_string;
            > unset($new_stri ng);[/color]

            I would use temporary arrays and the implode() function, and a switch
            statement instead of the if/elseif:

            <?php
            $string = "Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev,
            Karen, Shawyer, Andrea";
            $array = explode ( ', ', $string );
            $tmp = array();
            $tmp2 = array();
            for( $i=0; $i<count($array ); $i++ ) {
            switch ($i % 2) {
            case 0:
            $tmp2[] = trim($array[$i]);
            break;
            case 1:
            $tmp2[] = trim($array[$i]);
            $tmp[] = implode(', ',$tmp2);
            $tmp2 = array();
            break;
            }
            }
            echo 'Before: ' . $string . "\n";
            $string = implode('; ',$tmp);
            echo 'After: ' . $string;
            ?>

            Ken

            Comment

            • Malcolm Dew-Jones

              #7
              Re: Change alternate commas to semi colons...

              Rik (luiheidsgoeroe @hotmail.com) wrote:
              : StevePBurgess@g mail.com wrote:
              : > With a string of authors such as:
              : >
              : > Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
              : > Shawyer, Andrea
              : >
              : > I would like to write a function to change every other comma into a
              : > semi colon (thereby defining where one name ends and the next begins).
              : >
              : > I could do it by writing a complex (and slow) procedure - but is there
              : > a quick way of doing it using regular expressions, for example?


              : Regular expressions can be used, I'd think this is faster:

              I wouldn't assume that a block of php code is faster than simply calling
              preg_replace. In fact I wouldn't be surprised if it's the other way round
              - preg_replace may be faster.

              preg_replace maps to a function written in C that does some string
              manipulations (all code written in C). All php does internally is to set
              up the parameters and call that underlying function.

              The block of php code, on the other hand, maps to a whole bunch of
              manipulations of php data structures driven by the php interpretter, each
              one of which likely involves setting up parameters and calling some
              underlying function to do the work.

              I would be interested to see the result of a proper comparison between the
              two.

              Comment

              • Bob Stearns

                #8
                Re: Change alternate commas to semi colons...

                StevePBurgess@g mail.com wrote:[color=blue]
                > With a string of authors such as:
                >
                > Carson, David, Milne, Rebecca, Pakes, Francis J., Shalev, Karen,
                > Shawyer, Andrea
                >
                > I would like to write a function to change every other comma into a
                > semi colon (thereby defining where one name ends and the next begins).
                >
                > I could do it by writing a complex (and slow) procedure - but is there
                > a quick way of doing it using regular expressions, for example?
                >
                > Many thanks.
                >[/color]
                Something like the following should do:

                $names = ereg_replace("\ ([^,]*,[^,]*\),", "\\1;", $names)

                Comment

                • StevePBurgess@gmail.com

                  #9
                  Re: Change alternate commas to semi colons...

                  Thanks for all the excellent suggestions!

                  Best regards,
                  Steve

                  Comment

                  • Andy Jeffries

                    #10
                    Re: Change alternate commas to semi colons...

                    On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote:[color=blue]
                    > : Regular expressions can be used, I'd think this is faster:
                    >
                    > I wouldn't assume that a block of php code is faster than simply calling
                    > preg_replace. In fact I wouldn't be surprised if it's the other way round
                    > - preg_replace may be faster.
                    >
                    > I would be interested to see the result of a proper comparison between the
                    > two.[/color]

                    Ask and ye shall receive:

                    I've done a small script that runs each version 5000 times (never guess
                    which is faster, always test). And the results are:

                    Function call: 0.0749678611755 seconds
                    PREG Replace: 0.0623338222504 seconds

                    So the preg version is faster (it completes in 83% of the time).

                    Now, there's another upside. In the way that Rik coded them, there's also
                    a subtle difference in the two outputs. The Function call version puts an
                    extra ; on the end (which would result in an empty element if exploded and
                    would therefore need to be tested for before creating a DB row or the like).

                    Also, that test version used the original authors string. If I make this
                    string 10 times as long, the times are as follows:

                    Function call: 0.81055188179 seconds
                    PREG Replace: 0.196034908295 seconds

                    So, the preg version absolutely whips the function call (completes in 24%
                    of the time).

                    As much as I'd advocate testing over guesswork, I agree with your
                    understanding of why it's much faster.

                    Hope this is interesting to someone.

                    Cheers,


                    Andy

                    --
                    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                    http://www.gphpedit.org | PHP editor for Gnome 2
                    http://www.andyjeffries.co.uk | Personal site and photos

                    Comment

                    • Andy Jeffries

                      #11
                      Re: Change alternate commas to semi colons...

                      On Tue, 20 Jun 2006 21:41:52 +0200, Juliette wrote:[color=blue]
                      > Because he wants to change every *other* comma into a semi-colon (i.e.
                      > change the second, fourth etc).
                      > Your function would change *every* comma into a semi-colon.
                      >
                      > I don't think you can easily do it with regex as you can't really
                      > differentiate between comma's.[/color]

                      Hi Juliette,

                      As Rik already posted, you certainly can do it with regexes and as I
                      posted, they're much faster than the function call.

                      There's a syntax error in your script:
                      [color=blue]
                      > for( $i=0; $i<$arraycount; $i++; ) {[/color]

                      should be:
                      [color=blue]
                      > for( $i=0; $i<$arraycount; $i++ ) {[/color]

                      And a bug that actually gives the incorrect output. The first part of the
                      string from your function is:

                      Carson; David, Milne; Rebecca,

                      And it should be:

                      Carson, David; Milne, Rebecca;

                      For reference, against the larger data set from my timing post, your
                      function completes in 0.5357 seconds (compared to 0.8106 for Rik's
                      function version and 0.1960 for Rik's regex).

                      Cheers,


                      Andy


                      --
                      Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                      http://www.gphpedit.org | PHP editor for Gnome 2
                      http://www.andyjeffries.co.uk | Personal site and photos

                      Comment

                      • Andy Jeffries

                        #12
                        Re: Change alternate commas to semi colons...

                        On Wed, 21 Jun 2006 08:09:39 +0000, Andy Jeffries wrote:[color=blue]
                        > And a bug that actually gives the incorrect output. The first part of the
                        > string from your function is:
                        >
                        > Carson; David, Milne; Rebecca,
                        >
                        > And it should be:
                        >
                        > Carson, David; Milne, Rebecca;[/color]

                        And it also has the same deficiency as Rik's function version in that it
                        adds an extra delimiter on the end.

                        Cheers,


                        Andy

                        --
                        Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                        http://www.gphpedit.org | PHP editor for Gnome 2
                        http://www.andyjeffries.co.uk | Personal site and photos

                        Comment

                        • Andy Jeffries

                          #13
                          Re: Change alternate commas to semi colons...

                          On Tue, 20 Jun 2006 13:14:20 -0700, Ken Robinson wrote:[color=blue]
                          > I would use temporary arrays and the implode() function, and a switch
                          > statement instead of the if/elseif:[/color]

                          Hi Ken,

                          OK, your function is the slowest of those posted so far, completing the
                          larger data set from my timing post in 0.9498 seconds (compared to 0.5357
                          seconds for Juliette, 0.8106 for Rik's function version and
                          0.1960 for Rik's regex).

                          But yours also has no errors and produces the correct output (with the
                          only other one being Rik's regex version).

                          Cheers,


                          Andy


                          --
                          Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                          http://www.gphpedit.org | PHP editor for Gnome 2
                          http://www.andyjeffries.co.uk | Personal site and photos

                          Comment

                          • Bent Stigsen

                            #14
                            Re: Change alternate commas to semi colons...

                            Andy Jeffries wrote:
                            [color=blue]
                            > On Tue, 20 Jun 2006 14:10:48 -0800, Malcolm Dew-Jones wrote:[color=green]
                            >> : Regular expressions can be used, I'd think this is faster:
                            >>
                            >> I wouldn't assume that a block of php code is faster than simply calling
                            >> preg_replace. In fact I wouldn't be surprised if it's the other way
                            >> round - preg_replace may be faster.
                            >>
                            >> I would be interested to see the result of a proper comparison between
                            >> the two.[/color][/color]
                            [snip][color=blue]
                            > Also, that test version used the original authors string. If I make this
                            > string 10 times as long, the times are as follows:
                            >
                            > Function call: 0.81055188179 seconds
                            > PREG Replace: 0.196034908295 seconds
                            >
                            > So, the preg version absolutely whips the function call (completes in 24%
                            > of the time).
                            >
                            > As much as I'd advocate testing over guesswork, I agree with your
                            > understanding of why it's much faster.[/color]

                            Indeed preg_replace has the advantage of doing its looping in compiled code,
                            but throwing around with arrays is not cheap, so I think the methods are
                            too different to make a fair comparison.

                            I would think that in this case, since the logic is quite simple, doing a
                            straightforward character by character sweep, just like preg_replace would
                            do for its pattern matching, ought to be faster, unless PHP really really
                            *really* stinks at doing loops in phpcode of course.

                            Can you run the same test with this code:

                            $alternate_comm a = false;
                            $length = strlen($authors );
                            for ($i=0; $i<$length; $i++) {
                            if ($authors[$i]==',') {
                            if ($alternate_com ma) $authors[$i] = ';';
                            $alternate_comm a = !$alternate_com ma;
                            }
                            }

                            Usually I would use strpos for this sort of thing, which could be even
                            faster, since it will shorten the looping in php, but still would depend on
                            how well php does it.

                            --
                            /Bent

                            Comment

                            • Geoff Berrow

                              #15
                              Re: Change alternate commas to semi colons...

                              Message-ID: <pan.2006.06.21 .08.03.50.58507 3@andyjeffries. co.uk> from
                              Andy Jeffries contained the following:
                              [color=blue]
                              >As much as I'd advocate testing over guesswork, I agree with your
                              >understandin g of why it's much faster.
                              >
                              >Hope this is interesting to someone.[/color]

                              It is to me, I'd always thought regexs were slower. Bugger, that means
                              I will /really/ have to get round to learning them.

                              --
                              Geoff Berrow (put thecat out to email)
                              It's only Usenet, no one dies.
                              My opinions, not the committee's, mine.
                              Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                              Comment

                              Working...