in reply to gimme some sed

Being cutesie is all very fine so long as you give us the actual information - in this case the error message.

There are a number of issues with the code snippet as shown:

$arg1 = $ARGV[0];

assigns the first parameter to $arg1 as I'm sure you intend, but doesn't remove the parameter from the argument list. If you want to remove the first arg you should:

my $arg1 = shift @ARGV;

The second line:

$restargs = @ARGV;

assigns the number of parameters to $restargs, which I'm fairly sure is not what you intend. An array in scalar context returns the number of items in the array. One of the following is more likely what you want:

my @restargs = @ARGV[1..$#ARGV]; # An array of all the args except the + first (uses array slice) my $restargs = "@ARGV"; # A string containing all the args concatenate +d together with spaces between them my $restargs = "@ARGV[1..$#ARGV]"; # As above excluding the first argu +ment

and finally

$restargs = ~s/$arg1 //g;

is not at all what you expect. The space between = and ~ turns the expression into an assignment to $restargs of the complement of the success result of applying the substitution to the default variable ($_).

I strongly recommend that you use strict; use warnings in all the Perl you write!

Update: add shift example


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: gimme some sed
by perlAffen (Sexton) on Oct 06, 2006 at 13:55 UTC
    well I did include the error message. anyway I guess I was confused by the fact that you can 'print' @ARGV and the output is all the arguments. I guess I thought I was trying to do this --> $allargs = `print @ARGV` which I see I can achieve by saying $allargs = "@ARGV"; These are subtleties I miss, though it makes sense. While I am using the first reply option, I see that I can use my original method with your tips.
    my $arg1 = $ARGV[0]; my $restargs = "@ARGV"; $restargs=~s/$arg1 //g;
    Is there any harm in dispensing with spaces around = marks apart from readability ? BTW Goedel could have used a sanity check tool.

      Is there any harm in dispensing with spaces around = marks apart from readability ?

      Putting -- or not putting -- whitespace around most operators is largely a matter of taste. (One must put whitespace around operators like or, gt, etc). Putting whitespace within operators is a bad idea; =~ is one operator; = ~ is two. If your editor does stuff like adding spaces where they don't belong, I'd suggest a new one.

      emc

      At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

      —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

      Whileitisamatteroftastetodispensewithspaces,personallyIfindithelpsreadabilitytousethem.


      DWIM is Perl's answer to Gödel