Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: compute the occurrence of words ("under stand arrays")

by ww (Archbishop)
on Feb 13, 2013 at 14:32 UTC ( [id://1018547]=note: print w/replies, xml ) Need Help??


in reply to compute the occurrence of words

First: I second Utilitarian's (implied) advice re hashes.

Second: The shebang line is unnecessary under windows unless you have trailing modifies ( like  -T or  -w ) and definitely doesn't need escaped backslashes.

Third: these are a couple ways to do PART of what your code tries to do -- count the words.

#!/usr/bin/perl use 5.016; use strict; use warnings; #1018535 =head NO ARRAY METHOD (everything between here and the =cut line is +commented out) my $count; while ( <DATA> ) { for ( $_ =~ /\w+/g ) { $count++; } } print "COUNT: $count\n"; =cut END of (POD-syntax code which is tantamount to a) multi-line + comment # you could stuff the individual (not necessarily unique) words into a +n array, this way: # ie, this uses an ARRAY. Run the code to see what's happened here. my $i = 0; my ( $count, @count); while ( <DATA> ) { for ( $_ =~ /(\w+)/g ) { push @count, "$_ $i"; $i ++; } } for $count(@count) { say "Array element is: $count"; } __DATA__ This these that the and how who writ this code how now brown cow the fox jumped into the hencoop the lazy brown dog was azleep.

Note that the highest numeric value from the array method is 25, even though there are 26 words in the __DATA__ section. Look at the first value of the output and you'll find a zero... which is the way array elements are denominated -- the first element is $array[0].

If you wish to see the NON-ARRAY method used, move the =head and =cut down to surround the second (and currently operative) code. The data section must, of course, stay outside this hackish method of doing a multi-line comment in a script.

BUT, to identify and count unique words using the commonly recommended approach, use a hash.

And finally, if by any chance you mean you're seeing things like HASH(0x213F7) when you mention "numeric values," (though I don't see why) what you're seeing is effectively a pointer to the memory where the actual values are stored; to understand that (and how to get the actual values) you'll need to study referencing and dereferencing, in our estimable Tutorials section.


If you didn't program your executable by toggling in binary, it wasn't really programming!

Replies are listed 'Best First'.
Re^2: compute the occurrence of words ("under stand arrays")
by BigGer (Novice) on Feb 13, 2013 at 15:14 UTC

    That's Brilliant I have both your examples working thanks. I will take your advise and read up on using hash. Thanks Again. G

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1018547]
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: (1)
As of 2024-04-25 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found