Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Cultural and Bibliometric Perl

by jeroenes (Priest)
on Jun 29, 2001 at 15:26 UTC ( [id://92583]=note: print w/replies, xml ) Need Help??


in reply to Cultural and Bibliometric Perl

Some comments:
  1. use strict warnings and diagnostics or die
  2. The file regex may produce some unwanted results with unix-like filenames: file.txt.bak . Just think which part you want to retain. You can find the first and last dot with index and rindex, respectively.
  3. You slurp the contents in array context only to join the array. You can also set $/ to undef:
    { local $/ = undef; $contenido = <LIBRO>; }
    You can leave the newlines intact, they will be catched with '\s'. Even better, tr will take care of that.
  4. Use lc or uc to change the case.
  5. You can simplify the translation, by complementing the list to the alphabetic range (see perlop):
    $contenido = uc $contenido; $contenido =~ tr/A-Z/ /cs;
  6. Use '\s+' rather than '\s', so you don't have to test for empty cases.
  7. You can get the total number without array assignment: $npalabras = keys %PF;The scalar context will force immediate size return.
  8. I would print LIBROUT in the while loop, so the system will get the chance to buffer nicely.
It's quite a list, but I hope it will give you the chance to learn new idiom. Result:
#.... my $contenido; { local $/ = undef; $contenido = <LIBRO>; } $contenido = uc $contenido; $contenido =~ tr/A-Z/ /cs; my %PF; $PF{$_}++ for( split /\s+/, $contenido); open LIBROUT, ">$ar.csv"; my $npalabras = keys %PF; while( keys %PF ){ print LIBROUT join ';', $_, my $f=$PF{$_}, $f/ $npalabras; print LIBROUT "\n"; }
Well, you see how the use of $_ simplifies things..

Hope this helps,

Jeroen
"We are not alone"(FZ)

Replies are listed 'Best First'.
Re: Re: Cultural and Bibliometric Perl
by Ignatius Monk (Novice) on Jun 29, 2001 at 15:53 UTC
    Dear friend:
    Thanks a lot by your suggestions :). I made this code at the beggining of my perl efforts and I hadnīt open It until I pasted It on the post, and of course the code was very simple :) I can see that your is much better.
    I'm just going to buy on the net the 3rd edition of the programming perl (because it hasn't arrived to the Spanish market by now) to have a better knowledge of the programming language :) the problem is that I'm totally self-teached on computer sciences (I program on VB C and now Perl but learning on my own way, buying books, downloading tutorials, etc) and I advance too slow sometimes, I suppose that with that reference book I'll learn better 'programming skills' from now on ;)
    Best regards
    Ignatius, the Ciberlibrarian Monk on the Perl Order ;)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-24 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found