Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Fetching unique owner name

by Anonymous Monk
on Dec 03, 2002 at 13:18 UTC ( [id://217209]=perlquestion: print w/replies, xml ) Need Help??

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

On my Solaris 7 unix.
I am trying to fetch a unique name of html or htm owners. Basically get names of all our web authors and a total count.

From my "ls -l" I just need the owner of the html or htm file and get a person's name just once where I dont see two of the same names. For example if I get these names for all html or htm owners:
Jones Jones Smith Smith Richards
I only need unique names such as:
Jones Smith Richards
My attempt to do it in Perl:
#!/usr/local/bin/perl -w open STDERR, "> /dev/null" || die "Cant redirect standrd error: $!"; use File::Find; use strict; print "Web owners\n"; print "----------\n\n"; my $ct = 0; sub search { if( $_ =~ /\.html?$/) { $datetime = scalar(localtime(time)); my $name = $File::Find::name; open ( F, $name ) or die "$!: $name\n"; while(my $owner = <F>) { ($ownerid, $groupid) = (stat( "$owner" ))[4]; $ownerName = getpwuid($ownerid); { $ownerName{$1} = 1; } } close F; } } my $dirs = '/thisdirectory/directory'; find( \&search, $dirs)); foreach (keys %ownername) { print "$_\n"; } print "$ct\n";

Replies are listed 'Best First'.
Re: Fetching unique owner name
by JaWi (Hermit) on Dec 03, 2002 at 13:35 UTC
    Hi,

    update: Actually tested the code and refined it a bit...
    update2: Crafted a better perl solution, not calling perl in Perl...

    Couldn't you just do something like:

    my ( @temp, %authors ); @temp = sort map { my @b=split; $b[2]; } `ls -l`; foreach ( @temp ) { $authors{$}=1 if not $authors{$_} and length $_; } print "$_\n" for keys %authors;'
    IIRC, uniq is a standard unix command, which is also available under Solaris 7.

    Greets,

    -- JaWi

    "A chicken is an egg's way of producing more eggs."

      HOw would this work? Am I in the right direction??
      @temp = '/mydirectory/directory'; my ( @temp, %authors ); @temp = sort map { my @b=split; $b[2]; } `ls -l`; foreach ( @temp ) { $authors{$}=1 if not $authors{$_} and length $_; } print "$_\n" for keys %authors;'
        Hi,

        Sorry, I (wrongly) assumed you were an advanced Perl monk. I'll explain my solution step by step:

        my ( @temp, %authors ); @temp = sort map { my @b=split; $b[2]; } `ls -l /path/to/authors`;
        (NB: note the added path, I forgot that in my original answer!)
        You should read this from right to left: each line of the output of the ls command is split and a map is created of each second element (ie. the user names). The array returned by map is sorted and assigned to @temp.
        The array @temp now contains all authors of the files in your directory, including doubles!

        foreach ( @temp ) { $authors{$}=1 if not $authors{$_} and length $_; }
        This will loop through @temp and make a hash with the author names as key. The value is only stored if it doesn't exists already and has a length of > 0.
        I do this to ensure the hash only contains unique names.

        print "$_\n" for keys %authors;
        This loops through the keys of the hash %authors and prints them. To get an array of the unique authors of the files in your directory you would do something like my @unique_authors = keys %authors;

        Hope this helps,

        -- JaWi

        "A chicken is an egg's way of producing more eggs."

Re: Fetching unique owner name
by Callum (Chaplain) on Dec 03, 2002 at 13:28 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://217209]
Approved by claree0
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2024-04-23 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found