inkedmn has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: how do i do this?
by stefp (Vicar) on Sep 29, 2001 at 02:46 UTC
    I am not sure what you meant by "being returned" Does that fit the bill?
    $,=' '; print sort @ARGV;
    @ARGV and $, are documented in perlvar.
    And, please, use meaningful subjects. Here something like "how to sort command line parameters?" would be nice.

    -- stefp

      for example, i type this at the command line: perl script1.pl apple radio bubble ham zebra and i want the output to be: apple bubble ham radio zebra i apologize for not explaining my question more clearly... thanks for your replies.
        shove the code below in the file script1.pl and it should do what you want.
        #/usr/bin/perl use strict; # a good habit to take use warnings; # except in production $,=' '; print sort @ARGV;

        -- stefp

Re: how do i do this?
by runrig (Abbot) on Sep 29, 2001 at 02:47 UTC
Re: how do i do this?
by cricket (Acolyte) on Sep 30, 2001 at 08:40 UTC
    yay, i just learned $, from stepf and runrig. i had done it like this:
    use strict; my @sorted_args = sort @ARGV; print "@sorted_args" . "\n";
    which could be nice if you need to use the sorted list more than once in the program.

    update:
    thanks blake for the additional examples and warnings about using $, . sorry if i was unclear above -- i meant that i was posting the way i had done it prior to learning about $,

    --cricket
    i am smaller than a grasshopper

      You mentioned $, but then never used it in your code... In fact, the print statement you have wouldn't be affected by $, anyway. $, is what print displays between its parameters: print $a, $b will actually print $a followed by $, followed by $b.

      The special variable for array interpolation is $". It is what you would need to set to change what happens in your print statement.

      However, anytime you hear "special variable" you need to realize that they are global, and you should avoid changing their global value. This can be done by localized them inside of an enclosing block. If you don't do this, you will introduce hard-to-track-down bugs in other parts of your program. Here is an example using the tips above....

      #!/usr/bin/perl -wT use strict; { local $, = ', '; my @sorted_args = sort @ARGV; print @sorted_args; } print "\nBack to normal: ", @ARGV, "\n"; { local $" = '-'; my @sorted_args = sort @ARGV; print "@sorted_args"; } print "\nBack to normal: @ARGV\n"; =OUTPUT % ./sortedargs.pl c a b a, b, c Back to normal: cab a-b-c Back to normal: c a b

      -Blake