[25922] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8144 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 3 03:05:35 2005

Date: Fri, 3 Jun 2005 00:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 3 Jun 2005     Volume: 10 Number: 8144

Today's topics:
        counting word occurances  <rodrick.brown@gmail.com>
    Re: counting word occurances  <jurgenex@hotmail.com>
    Re: counting word occurances <john@castleamber.com>
    Re: counting word occurances <john@castleamber.com>
    Re: counting word occurances <1usa@llenroc.ude.invalid>
        DBI - DBD-DB2 Problem - Please help <dieter.brennsteiner@sbg.at>
    Re: DBI - DBD-DB2 Problem - Please help <glex_no-spam@qwest-spam-no.invalid>
    Re: DBI - DBD-DB2 Problem - Please help <jleffler@earthlink.net>
    Re: DBI - DBD-DB2 Problem - Please help <dieter.brennsteiner@bs-ag.com>
    Re: Neat way of setting default values <xennar@yahoo.com>
    Re: Suppression of error messages if a regex does not m <comdog@panix.com>
        Using Mail::IMAPClient and Mail::Folder together usenet@isbd.co.uk
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 03 Jun 2005 03:28:08 GMT
From: "Rodrick Brown" <rodrick.brown@gmail.com>
Subject: counting word occurances 
Message-Id: <c7Qne.4832$jU5.1473111@twister.nyc.rr.com>

Hello,

Just learning Perl so bare with me.

I have the following output file:

pear
 apple
apple
   orange
mango
mango
        pear
   cherry
apple

ill would like the count the ammount of occurances for each fruit.

I spent a few hours trying to do this and just gave up if someone can help 
me out with an example or a better way to do this than the method i'm trying 
to use

This is as far as I got

#!/usr/bin/perl -w

use strict;

my @keys;
my @fruits;
my %cnt;
my $types;
my $f = 1;
my $m;

open(LOG,"/tmp/fruits.txt") || die("Can't open file: $!\n");
while(<LOG>)
{
        next if(/^\s+/);
        push(@fruits,$_);
}

# Give all fruits a default value of 1
foreach my $types (@fruits)
{
  $cnt{$types} = $f;
}

foreach $types (@fruits)
{
        @keys = keys %cnt;
        while(@keys)
        {
                my $fruitnames = pop(@keys);
                if($types =~ m/$fruitnames/)
                {
                        $cnt{$types}++;
                        print "$cnt{$types} $fruitnames";
                }
        }
}

The code doesnt work and i'm a bit fustrated that I couldnt get it working, 
many times I thought I had it but I never did get the results I expected.

-- 
RB 




------------------------------

Date: Fri, 03 Jun 2005 04:46:11 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: counting word occurances 
Message-Id: <ngRne.38532$GN3.26737@trnddc04>

Rodrick Brown wrote:
> Just learning Perl so bare with me.

There isn't really much Perl involved here except for the hash.

> I have the following output file:

I guess you mean input file?

> pear
> apple
> apple
>   orange
> mango
> mango
>        pear
>   cherry
> apple
>
> ill would like the count the ammount of occurances for each fruit.
>
> I spent a few hours trying to do this and just gave up if someone can
> help me out with an example or a better way to do this than the
> method i'm trying to use
>
> This is as far as I got
[code snipped]

Sorry, this is so convoluted, I'm not even trying to understand what you may 
have been thinking when writing it.

The following code works:

use warnings; use strict;
my %cnt;
open(LOG,"/tmp/fruits.txt") or die("Can't open file: $!\n");
while(<LOG>){
    s/^\s*//; #remove leading white space
    s/\s*$//; #remove trailing white space
    $cnt{$_}++; #count this fruit
}
delete $cnt{''}; #delete empty key in case we picked up an empty line

for (keys(%cnt)){#print the whole set
    print "$cnt{$_} $_\n";
}

jue 




------------------------------

Date: 3 Jun 2005 04:25:24 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: counting word occurances
Message-Id: <Xns9669EE2CB4676castleamber@130.133.1.4>

Rodrick Brown wrote:

> Hello,
> 
> Just learning Perl so bare with me.
> 
> I have the following output file:
> 
> pear
>  apple
> apple
>    orange
> mango
> mango
>         pear
>    cherry
> apple
> 
> ill would like the count the ammount of occurances for each fruit.
> 
> I spent a few hours trying to do this and just gave up if someone can
> help me out with an example or a better way to do this than the method
> i'm trying to use
> 
> This is as far as I got
> 
> #!/usr/bin/perl -w

don't use -w, use warnings; instead:

> use strict;

use warnings;

> my @keys;
> my @fruits;
> my %cnt;
> my $types;
> my $f = 1;
> my $m;

do this when you need it, not ahead of time. I replace them with:

my $filename = '/tmp/fruits.txt';
my %count;

> open(LOG,"/tmp/fruits.txt") || die("Can't open file: $!\n");

open my $fh, $filename or die "Can't open '$filename' for reading: $!";

while ( my $line = <$fh> ) {

    	$line =~ s/^\s+//;    	# remove leading whitespace
    	$line =~ s/\s+$//;    	# remove trailing whitespace (and \n)

    	next if $line eq '';    # skip empty lines

    	$count{ $line }++;
}

close $fh or die "Can't close '$filename' after reading: $!"

Note that some magic happens here and there, like incrementing an 
undefined entry in a hash table (%count) assumes it had a value of zero.

Also note that I use an undefined variable in open, so it can be used as 
a file handle.

> foreach $types (@fruits)
> {
>         @keys = keys %cnt;
>         while(@keys)
>         {
>                 my $fruitnames = pop(@keys);
>                 if($types =~ m/$fruitnames/)
>                 {
>                         $cnt{$types}++;
>                         print "$cnt{$types} $fruitnames";
>                 }
>         }
> }

I don't even want to guess what's going on here :-)

print "$count{$_} $_\n"
    	for sort { $count{ $b } <=> $count{ $a } } keys %count;

Since I have $b to the left, it sorts the keys of %count descending 
based on the count of each item. 

I recommend reading a bit more on hash tables, the use of for(each), and 
open.

(all code untested)

-- 
John                   Small Perl scripts: http://johnbokma.com/perl/
               Perl programmer available:     http://castleamber.com/
            Happy Customers: http://castleamber.com/testimonials.html
                        


------------------------------

Date: 3 Jun 2005 05:52:16 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: counting word occurances
Message-Id: <Xns966A8DC54ED4castleamber@130.133.1.4>

Jürgen Exner wrote:

> delete $cnt{''}; #delete empty key in case we picked up an empty line

Must remember that one, more readable then next if $line eq '';

-- 
John                   Small Perl scripts: http://johnbokma.com/perl/
               Perl programmer available:     http://castleamber.com/
            Happy Customers: http://castleamber.com/testimonials.html
                        


------------------------------

Date: Fri, 03 Jun 2005 06:19:14 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: counting word occurances
Message-Id: <Xns966A1762FD77Basu1cornelledu@127.0.0.1>

"Jürgen Exner" <jurgenex@hotmail.com> wrote in
news:ngRne.38532$GN3.26737@trnddc04: 

> Rodrick Brown wrote:
>> Just learning Perl so bare with me.

I'd rather not be naked with strangers ;)

 ...

>> This is as far as I got
> [code snipped]
> 
> Sorry, this is so convoluted, 

Agreed.

> while(<LOG>){
>     s/^\s*//; #remove leading white space
>     s/\s*$//; #remove trailing white space
>     $cnt{$_}++; #count this fruit
> }
> delete $cnt{''}; #delete empty key in case we picked up an empty line

Or:

while(<LOG>) {
   next unless /^\s*(\w+)\s*$/;
   $cnt{$1}++;
}

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


------------------------------

Date: Thu, 02 Jun 2005 22:07:13 +0200
From: Dieter Brensteiner <dieter.brennsteiner@sbg.at>
Subject: DBI - DBD-DB2 Problem - Please help
Message-Id: <VdJne.2$U32.1026115@news.salzburg-online.at>

Hi !

I can run my index.cgi program without error against a db2 Database from 
command line !
There are no errors at all.

When I try to run the index.cgi file on my apache redhat ws4 i get the 
following error:

install_driver(DB2) failed: Can't load 
'/usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi/auto/DBD/DB2/DB2.so' 
for module DBD::DB2: libdb2.so.1: cannot open shared object file: No 
such file or directory at 
/usr/lib/perl5/5.8.5/i386-linux-thread-multi/DynaLoader.pm line 230.
  at (eval 3) line 3
Compilation failed in require at (eval 3) line 3.
Perhaps a required shared library or dll isn't installed where expected
  at /var/www/htdocs/csc/index.cgi line 113


Please can anybody help ? Urgently


thanx guys...



------------------------------

Date: Thu, 02 Jun 2005 15:29:52 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: DBI - DBD-DB2 Problem - Please help
Message-Id: <4%Jne.2091$y05.5607@news.uswest.net>

Dieter Brensteiner wrote:
> Hi !
> 
> I can run my index.cgi program without error against a db2 Database from 
> command line !
> There are no errors at all.
> 
> When I try to run the index.cgi file on my apache redhat ws4 i get the 
> following error:
> 
> install_driver(DB2) failed: Can't load 
> '/usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi/auto/DBD/DB2/DB2.so' 
> for module DBD::DB2: libdb2.so.1: cannot open shared object file: No 
> such file or directory at 
> /usr/lib/perl5/5.8.5/i386-linux-thread-multi/DynaLoader.pm line 230.
>  at (eval 3) line 3
> Compilation failed in require at (eval 3) line 3.
> Perhaps a required shared library or dll isn't installed where expected
>  at /var/www/htdocs/csc/index.cgi line 113
> 
> 
> Please can anybody help ? Urgently
> 
> 
> thanx guys...
> 

Possibly your web server is running a different version of perl. 
Typically it's due to your PATH environment variable being different 
from the user running the web server.

If running "perl -v", from your command line doesn't show something 
like: "This is perl, v5.8.5...", that's the issue.  The best fix for 
that is to install DBD::DB2 using the perl5.8.5 version.

/path/to/bin/perl5.8.5 Makefile.PL
etc.

or use CPAN.


------------------------------

Date: Fri, 03 Jun 2005 05:14:15 GMT
From: Jonathan Leffler <jleffler@earthlink.net>
Subject: Re: DBI - DBD-DB2 Problem - Please help
Message-Id: <HGRne.4531$s64.1411@newsread1.news.pas.earthlink.net>

Dieter Brensteiner wrote:
> I can run my index.cgi program without error against a db2 Database from 
> command line ! There are no errors at all.

Good - so you know what the correct environment should be.

> When I try to run the index.cgi file on my apache redhat ws4 i get the 
> following error:
> 
> install_driver(DB2) failed: Can't load 
> '/usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi/auto/DBD/DB2/DB2.so' 
> for module DBD::DB2: libdb2.so.1: cannot open shared object file: No 
> such file or directory at 
> /usr/lib/perl5/5.8.5/i386-linux-thread-multi/DynaLoader.pm line 230.
>  at (eval 3) line 3
> Compilation failed in require at (eval 3) line 3.
> Perhaps a required shared library or dll isn't installed where expected
>  at /var/www/htdocs/csc/index.cgi line 113
> 
> 
> Please can anybody help ? Urgently

Most likely, the environment that works on the command line is not 
available in the web server environment.  Look up the PassEnv and SetEnv 
directives.

(The odds that a problem of the form 'it works on the command line and 
not via a web server' is caused by faulty environment settings are 
astoundingly high; it covers more than 99% of the cases I've seen.)

In case of doubt, write a web page that dumps the environment it 
receives.  Then compare that with your working environment.  The problem 
will, most likely, be obvious.


-- 
Jonathan Leffler                   #include <disclaimer.h>
Email: jleffler@earthlink.net, jleffler@us.ibm.com
Guardian of DBD::Informix v2005.01 -- http://dbi.perl.org/


------------------------------

Date: Fri, 03 Jun 2005 08:25:02 +0200
From: Dieter Brennsteiner <dieter.brennsteiner@bs-ag.com>
Subject: Re: DBI - DBD-DB2 Problem - Please help
Message-Id: <429ff7c1$0$16448$91cee783@newsreader02.highway.telekom.at>

J. Gleixner schrieb:
> Dieter Brensteiner wrote:
> 
>> Hi !
>>
>> I can run my index.cgi program without error against a db2 Database 
>> from command line !
>> There are no errors at all.
>>
>> When I try to run the index.cgi file on my apache redhat ws4 i get the 
>> following error:
>>
>> install_driver(DB2) failed: Can't load 
>> '/usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi/auto/DBD/DB2/DB2.so' 
>> for module DBD::DB2: libdb2.so.1: cannot open shared object file: No 
>> such file or directory at 
>> /usr/lib/perl5/5.8.5/i386-linux-thread-multi/DynaLoader.pm line 230.
>>  at (eval 3) line 3
>> Compilation failed in require at (eval 3) line 3.
>> Perhaps a required shared library or dll isn't installed where expected
>>  at /var/www/htdocs/csc/index.cgi line 113
>>
>>
>> Please can anybody help ? Urgently
>>
>>
>> thanx guys...
>>
> 
> Possibly your web server is running a different version of perl. 
> Typically it's due to your PATH environment variable being different 
> from the user running the web server.
> 
> If running "perl -v", from your command line doesn't show something 
> like: "This is perl, v5.8.5...", that's the issue.  The best fix for 
> that is to install DBD::DB2 using the perl5.8.5 version.
> 
> /path/to/bin/perl5.8.5 Makefile.PL
> etc.
> 
> or use CPAN.


Hi !

The output is:

This is perl, v5.8.5 built for i386-linux-thread-multi

Copyright 1987-2004, Larry Wall

Perl may be copied only under the terms of either the Artistic License 
or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.




So it is 5.8.5 . I checked already the environment - nothing that i 
should miss so far ?

Any other suggestions would be nice

thanx







------------------------------

Date: Thu, 02 Jun 2005 23:25:27 +0200
From: Xenna <xennar@yahoo.com>
Subject: Re: Neat way of setting default values
Message-Id: <429f7909$0$90716$e4fe514c@dreader7.news.xs4all.nl>

Brian McCauley wrote:
> 
> 
> Xenna wrote:
> 
>> Gunnar Hjalmarsson wrote:
>>
>>> Xenna wrote:
>>>
>>>> Gunnar Hjalmarsson wrote:
>>>>
>>>>> Berk Birand wrote:
>>>>>
>>>>>> Gunnar Hjalmarsson wrote:
>>>>>>
>>>>>>> Berk Birand wrote:
>>>>>>>
>>>>>>>>
>>>>>>>> my $tag = (get_mp3tag($file) or "No tag\n");
>>>>>>>
>>>>>>>
>>>>>>> Why isn't that smart enough?
>>>>>>
>>>>>>
>>>>>> It is not smart enough because it doesn't work.
>>>>>
>>>>>
>>>>> It works fine. So does:
>>>>>
>>>>>     my $tag = get_mp3tag($file) || "No tag\n";
>>>>
>>>>
>>>> Instead of kindly telling you what they know you want to hear they 
>>>> revel in explaining you how you stupidly misphrased your questions.
>>>
>>>
>>> Even if that may be true once in a while, I don't understand how it 
>>> would be applicable in this case. How did you figure out what the OP 
>>> "wants" to know? I still don't know what it is...
>>
>>
>> Well, Mark & Brian figured it out *and* were able to explain the 
>> underlying issue clearly.
> 
> 
> Er, no.  There was nothing wrong with the original code that Berk posted.

OK, so I must have misunderstood some things ;)

> Mark posted alternative version of the code that would work no better or 
> worse.
> 
> I (Brian) pointed out that Berk's original code was as far as perl is 
> concerned exactly the same as Mark's.  This is just what Gunnar did.

Well, Berk's code may have been correct but you were good enough to 
point out the precedence issue anyway ;)

> Nobody here (unless they've had private correspondance with Berk) can 
> know what the original problem was.

It would be interesting to find out....

Sorry for complicating things unnecessarily :}

X.


------------------------------

Date: Thu, 02 Jun 2005 15:14:18 -0500
From: brian d foy <comdog@panix.com>
Subject: Re: Suppression of error messages if a regex does not match
Message-Id: <020620051514180084%comdog@panix.com>

In article <d7n7u3$gg7$1@slavica.ukpost.com>, Brian McCauley
<nobull@mail.com> wrote:

> In the case of the 'numeric' and 'uninitialized' there can be _many_ 
> times when we do know better.  I would not suggest turning them off 
> globally [snip]

Indeed, and I think I should have emphasized that before. In those
cases I turn off warnings for that scope (or create a naked block
scope if possible so I can limit the effect).

-- 
brian d foy, bdfoy@cpan.org
Subscribe to The Perl Review: http://www.theperlreview.com


------------------------------

Date: 2 Jun 2005 20:57:49 GMT
From: usenet@isbd.co.uk
Subject: Using Mail::IMAPClient and Mail::Folder together
Message-Id: <3g9a6dFbbnboU1@individual.net>

I want to manipulate mail messages between IMAP servers and local
folders.  I have got Mail::IMAPClient working nicely and now want to
start on the local mail folders.

The Mail::Folder module uses a Mail::Internet object to hold the mail
messages it appends to and gets from folders.

How can I easily move messages between what's returned by IMAPClient's
get_body and get_header methods and Mail::Internet's append methods?

-- 
Chris Green


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8144
***************************************


home help back first fref pref prev next nref lref last post