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

#!/usr/bin/perl -w use strict; find_serv (); sub find_serv { @ARGV == 2 || die "usage: $0 INDEX_FILE '.com'\n"; my ( $INDEX_FILE, $com ) = @ARGV; my $list = "grep 'href=' $INDEX_FILE|" ; open (my $HAN, "$list" ) || die "Cannot open '$INDEX_FILE' because:: +$!"; while (<$HAN>) { chomp $_; my @array = split (/\//, $_); if ($array[2] =~ /$com/ ){ my $sorted = $array[2]; system (sort -u $scalar); # - doesn't work #print "$_\n" for sort { versioncmp($a,$b) } $sorted; }} } #END sorted file should look like www.google.ie www.icq.com www.....

Replies are listed 'Best First'.
Re: how to sort uniq $array[2]
by Corion (Patriarch) on Jun 05, 2011 at 20:12 UTC

    See perlop on qx or "backticks". Also see perlfaq4 on how to sort an array (by anything) and how to remove duplicates from a list.

      Thanks Corion -but i already did,that is why i'm here solution

      my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, uc( (/\d+\s*(\S+)/)[0]) ] } @sort;
      not working for me

        You didn't post this code when you asked your question.

        Please tell us how the code you have now fails to work for you, and what output you expect instead.

        I don't see, for example, where your current code would remove duplicates.

Re: how to sort uniq $array[2]
by wind (Priest) on Jun 05, 2011 at 21:57 UTC

    Please read How do I post a question effectively?.

    Below is your code reformatted for others to be able to help you:

    #!/usr/bin/perl -w use strict; find_serv (); sub find_serv { @ARGV == 2 || die "usage: $0 INDEX_FILE '.com'\n"; my ( $INDEX_FILE, $com ) = @ARGV; my $list = "grep 'href=' $INDEX_FILE|" ; open (my $HAN, "$list" ) || die "Cannot open '$INDEX_FILE' because +:: $!"; while (<$HAN>) { chomp $_; my @array = split (/\//, $_); if ($array2 =~ /$com/ ){ my $sorted = $array2; system (sort -u $scalar); # - doesn't work #print "$_\n" for sort { versioncmp($a,$b) } $sorted; } } } #END sorted file should look like www.google.ie www.icq.com www.....
Re: how to sort uniq $array[2]
by ww (Archbishop) on Jun 05, 2011 at 21:57 UTC
    Also see the instructions around the text entry box:
    Use <code>...</code> around code and data
    or words to that general effect. See also Markup in the Monastery.
Re: how to sort uniq $array[2]
by JavaFan (Canon) on Jun 06, 2011 at 09:03 UTC
    I've no idea what $scalar is supposed to be; but let's assume it's the name of a file you want to sort. You could do:
    use autodie ':all'; system "sort -u '$scalar' > '$scalar.sorted'"; rename "$scalar.sorted", $scalar;

      well $scalar we can just disregard - i wanted to sort @array2 , that is where www.*****.com servers will be after split function

      #!/usr/bin/perl -w use strict; find_serv (); sub find_serv { @ARGV == 2 || die "usage: $0 INDEX_FILE '.com'\n";# .com---->icq.com my ( $INDEX_FILE, $com ) = @ARGV; my $list = "grep 'href=' $INDEX_FILE|" ;#- $INDEX_FILE --> wget www. +icq.com open (my $HAN, "$list" ) || die "Cannot open '$INDEX_FILE' because:: +$!"; while (<$HAN>) { chomp $_; my @array = split (/\//, $_); if ($array[2] =~ /$com/ ){#matches icq.com my @sorted = $array[2]; system "sort -u '@sorted'"; #---not working }} }
        But what is $array[2]? A name of a file? In that case, take my previous answer, and replace $scalar with $array[2].