in reply to chomp a List

You cannot use chomp to erase characters you have already printed. For the logic of separating items by commas, Perl has the convenient join function. All you need to do is get the items you want to print into a list to pass to join. grep is a good way to do this:
my @dir = readdir DIR; my @to_print = grep { -r $_ && -w $_ && -x $_ && /^[a-zA-Z]/ } @dir; print join "," => @to_print;

blokhead

Replies are listed 'Best First'.
Re^2: chomp a List
by ZlR (Chaplain) on Feb 08, 2005 at 09:26 UTC
    print join "," => @to_print;
    I never saw this ! Does this means that one can use => instead of a coma everywhere ?
    Is there a precedence difference ?
    I'm used to do :
    print join(",", @to_print)
    which is obviously less idiomatic .

      => is almost the same as ,

      The difference is that => forces the value to the left of it to be interpreted as a quoted string

      This is why
      #!/usr/bin/perl -w my %a=( foo => "bar", fi , "fo");

      Will get you a warning for the unquoted fi, but not the unquoted foo. However, normal operator precedence still applies, so

      print join , => @to_print;
      won't work, whereas
      print join or => @to_print;
      will.
        cool :) or is seen as a string ... thx !
Re^2: chomp a List
by Anonymous Monk on Feb 08, 2005 at 15:57 UTC
    Thank you for the help its almost to simple.