Replace substring "_.modem_" with modem price from $price ?

Discussion in 'Perl Misc' started by Martina, Sep 9, 2005.

  1. Martina

    Martina Guest

    How to replace (change) some pattern in array with value of variable.
    Replace substring "_.modem_" with modem price from $price

    ----------------------
    @fileList=("Price for new modem is: _modem_?.", "Modem is price is:
    _modem_?.");
    $price=10;

    foreach $red(@fileList){@rast = split ("_modem_",$red);
    if ($#rast > 0) {
    print "$rast[0]$price$rast[1]\n";
    }
    }
    --------------

    Any better way to solve this problem?

    Tnx.
     
    Martina, Sep 9, 2005
    #1
    1. Advertising

  2. Martina

    Anno Siegel Guest

    Martina <> wrote in comp.lang.perl.misc:
    > How to replace (change) some pattern in array with value of variable.
    > Replace substring "_.modem_" with modem price from $price


    Please use warnings and strict in your code. In consequence, you'll
    have to declare your variables. Also your code would profit from
    some white space.

    Your code doesn't change anything, it only prints the changed strings.

    > ----------------------
    > @fileList=("Price for new modem is: _modem_?.", "Modem is price is:
    > _modem_?.");


    Is @fileList an appropriate name? The elements don't look like plausible
    file names.

    > $price=10;
    >
    > foreach $red(@fileList){@rast = split ("_modem_",$red);


    The first argument of split() is a regex, it should be written as such:

    ... split /_modem_/, $red;

    > if ($#rast > 0) {
    > print "$rast[0]$price$rast[1]\n";


    Since you already have the pieces in a list you can put them together
    with join();

    print join( $price, @rast), "\n";

    > }
    > }
    > --------------
    >
    > Any better way to solve this problem?


    If your problem description is correct, you don't have a solution yet.

    The normal way in Perl to change parts of a string is a substitution
    s///. This changes the elements of @fileList according to your
    description:

    my @fileList = (
    'Price for new modem is: _modem_?.',
    'Modem is price is: _modem_?.',
    );
    my $price = 10;

    s/_modem_/$price/ for @fileList;

    print "$_\n" for @fileList;

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Sep 9, 2005
    #2
    1. Advertising

  3. Martina

    Anno Siegel Guest

    David Serrano (Hue-Bond) <> wrote in comp.lang.perl.misc:
    > Anno Siegel, vie20050909@11:01:39(CEST):
    > >
    > > s/_modem_/$price/ for @fileList;
    > >
    > > print "$_\n" for @fileList;

    >
    > Just a note. I think walking the array only once would be better
    > performance-wise, wouldn't it?


    It might. Only a benchmark would tell. In combination with actual IO
    the difference isn't going to be large.

    I set the print statement it off in an extra loop because it is only there
    to show that the rest of the program worked. It shows an intent, efficiency
    wasn't on my mind.

    > for (@fileList) {
    > s/_modem_/$price/;
    > print "$_\n";
    > }
    >
    > Or, shorter;
    >
    > s/_modem_/$price/, print "$_\n" for @fileList;


    Yes, but it combines two actions one of which is likely to go away
    in a finished program.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Sep 10, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dominique Deleris
    Replies:
    4
    Views:
    498
  2. Paul
    Replies:
    19
    Views:
    13,237
    Netocrat
    Nov 10, 2005
  3. Nandhu
    Replies:
    0
    Views:
    481
    Nandhu
    Dec 4, 2007
  4. sp
    Replies:
    1
    Views:
    856
    Steven Cheng [MSFT]
    Aug 28, 2008
  5. Replies:
    3
    Views:
    232
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page