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

Re: Statistics from a txtfile

by suaveant (Parson)
on Dec 28, 2007 at 18:38 UTC ( [id://659374]=note: print w/replies, xml ) Need Help??


in reply to Statistics from a txtfile

you probably want a regexp more like
/^[_a-zA-Z][^.]{1,7}\.txt$/
This matches an underscore or letter (the {1} is unnecessary, char classes always match one char only, which is why you need modifiers like * or + to make it do something different)

Then you can do

while(<READFILE>) { # something here }
which will go through the file line by line putting the data in $_
I would read up on chomp and split and regular expressions on how to work with the data in the file.

P.S. put <code> tags around your code to make it display properly in a post.

                - Ant
                - Some of my best work - (1 2 3)

Replies are listed 'Best First'.
Re^2: Statistics from a txtfile
by mbdc566021 (Initiate) on Jan 02, 2008 at 18:27 UTC
    Have read up on regex and still confused.Am i anywhere near correct with the variables?
    #!/usr/bin/perl print("Please enter filename: "); $filename = <STDIN>; chomp ($filename); if ($filename=~m/^[_a-zA-Z][^.]{1,7}\.txt$/) { open (READFILE, $filename)|| die "Failed to open $filename: $!"; } my $filecontents; { local undef $/; $filecontents = <READFILE>; } close <READFILE>; #slurps the whole file into variable my @characters = $filecontents =~ m/./g; # puts a copy of each match + into @characters my $CharCount = scalar @characters; # this counts the number of + elements in @characters my @words = $filecontents =~m/\b\s/g; # number of words my @paragraph =($ # number of paragraphs. wha +t is code for new line char ie carriage return? my @sentences = $filecontents=~m/\.$/g; # number of sentences for (@characters){ print "$_ \n"; } # this will print a list +of each item counted: # output data # open(OUT, ">data1.txt") || die "data1.txt not open: $!"; # close(OUT);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 05:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found