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

Perl Sub using variables like \1 \2 and combining that value with a numeric

Expand Messages
  • campbell262
    I have records in a file that look like this: IPOD,YAHOO,0033092909,50/4Z1,technology_102 I would like to convert the 4Z1 to 42001 so the complete record would
    Message 1 of 3 , Aug 12, 2010
      I have records in a file that look like this:
      IPOD,YAHOO,0033092909,50/4Z1,technology_102

      I would like to convert the 4Z1 to 42001 so the complete record would look like this:

      IPOD,YAHOO,0033092909,50/42001,technology_

      So essentially what I am trying to do is find records that have a slash / and then one or more digits followed by Z1, and convert the Z1 value to 2001, while retaining the /and the digits that proceeded the Z.

      What I have tried is:

      perl -pi -e "s#((/\d)Z1,)#\22001,#g" test

      But I get:
      IPOD,YAHOO,0033092909,50\22001,technology_102

      so the value stored in \2 is getting lost.

      if i try this perl command (thinking i need to concat the values together) I get a mysterious 4 (how?) followed by a period and then 2001 :
      perl -pi -e "s#((/\d)Z1,)#\2.2001,#g"

      IPOD,YAHOO,0033092909,50/4.2001,technology_102

      Can someone help with this and explain what I am missing in the concept of using the /1 .../n values when wanting to put them with digits.
    • merlyn@stonehenge.com
      ... campbell262 so the value stored in 2 is getting lost. 2 on the right-hand side has been deprecated for 15 years or so. You want $2 on the right side.
      Message 2 of 3 , Aug 12, 2010
        >>>>> "campbell262" == campbell262 <campbell262@...> writes:

        campbell262> so the value stored in \2 is getting lost.

        \2 on the right-hand side has been deprecated for 15 years or so.
        You want $2 on the right side.

        And you'll also want to know this:

        **************************************************
        *
        * THIS IS NOT THE OFFICIAL perl BEGINNERS GROUP.
        *
        * The official one is at beginners@..., with instructions on how to
        * join at http://lists.perl.org/list/beginners.html.
        *
        * You'll get far better response there, not to say you won't get *any* here.
        *
        **************************************************


        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
      • Laszlo
        This works $t = IPOD,YAHOO,0033092909,50/4Z1,technology_102 ; $z = 200 ; $t =~ s/(.*?) /([0-9])Z(.*?)$/$1 /$2$z$3/; print $t
        Message 3 of 3 , Aug 18, 2010
          This works

          $t = "IPOD,YAHOO,0033092909,50/4Z1,technology_102";
          $z = '200';
          $t =~ s/(.*?)\/([0-9])Z(.*?)$/$1\/$2$z$3/;
          print $t #IPOD,YAHOO,0033092909,50/42001,technology_102


          --- In perl-beginner@yahoogroups.com, "campbell262" <campbell262@...> wrote:
          >
          > I have records in a file that look like this:
          > IPOD,YAHOO,0033092909,50/4Z1,technology_102
          >
          > I would like to convert the 4Z1 to 42001 so the complete record would look like this:
          >
          > IPOD,YAHOO,0033092909,50/42001,technology_
          >
          > So essentially what I am trying to do is find records that have a slash / and then one or more digits followed by Z1, and convert the Z1 value to 2001, while retaining the /and the digits that proceeded the Z.
          >
          > What I have tried is:
          >
          > perl -pi -e "s#((/\d)Z1,)#\22001,#g" test
          >
          > But I get:
          > IPOD,YAHOO,0033092909,50\22001,technology_102
          >
          > so the value stored in \2 is getting lost.
          >
          > if i try this perl command (thinking i need to concat the values together) I get a mysterious 4 (how?) followed by a period and then 2001 :
          > perl -pi -e "s#((/\d)Z1,)#\2.2001,#g"
          >
          > IPOD,YAHOO,0033092909,50/4.2001,technology_102
          >
          > Can someone help with this and explain what I am missing in the concept of using the /1 .../n values when wanting to put them with digits.
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.