Loading ...
Sorry, an error occurred while loading the content.

inserting chars into string based on length

Expand Messages
  • calcfreak2003
    im trying to take a string(of variable length) and put ,% in front of every 8th character like $input_string = 000000000000000000 ; #routine here
    Message 1 of 3 , Apr 2, 2005
    • 0 Attachment
      im trying to take a string(of variable length)
      and put ",%" in front of every 8th character

      like



      $input_string = "000000000000000000";

      #routine here

      output_string = "00000000,%00000000,%00";
    • Charles K. Clarkson
      ... my $string = 000000000000000000 ; $string =~ s/(.{8})/$1,%/g; print $string; For details read perlre and perlop: Quote and Quote-like Operators. HTH,
      Message 2 of 3 , Apr 3, 2005
      • 0 Attachment
        calcfreak2003 <mailto:calcfreak2003@...> wrote:

        : im trying to take a string(of variable length)
        : and put ",%" in front of every 8th character
        :
        : like
        :
        :
        :
        : $input_string = "000000000000000000";
        :
        : #routine here
        :
        : output_string = "00000000,%00000000,%00";


        my $string = "000000000000000000";

        $string =~ s/(.{8})/$1,%/g;

        print $string;

        For details read perlre and perlop: Quote and Quote-like Operators.


        HTH,

        Charles K. Clarkson
        --
        Mobile Homes Specialist
        254 968-8328
      • Jonathan Paton
        ... Try: s/(.{8})/$1,%/g; But you need to add zero length assertions if you want to avoid placing ,% at the end of a string when the number of characters is a
        Message 3 of 3 , Apr 3, 2005
        • 0 Attachment
          > im trying to take a string(of variable length)
          > and put ",%" in front of every 8th character
          >
          > like
          >
          > $input_string = "000000000000000000";
          >
          > #routine here
          >
          > output_string = "00000000,%00000000,%00";

          Try:

          s/(.{8})/$1,%/g;

          But you need to add zero length assertions if you want to
          avoid placing ,% at the end of a string when the number of
          characters is a multiple of eight.

          Study the above (which works... tested), and figure out what
          is being done. Also find out about $1 and \1, and whether
          I am using the correct one in my example...

          Jonathan Paton

          --
          #!perl
          $J=' 'x25 ;for (qq< 1+10 9+14 5-10 50-9 7+13 2-18 6+13
          17+6 02+1 2-10 00+4 00+8 3-13 3+12 01-5 2-10 01+1 03+4
          00+4 00+8 1-21 01+1 00+5 01-7 >=~/ \S\S \S\S /gx) {m/(
          \d+) (.+) /x,, vec$ J,$p +=$2 ,8,= $c+= +$1} warn $J,,
        Your message has been successfully submitted and would be delivered to recipients shortly.