hi, have a look at my code below. what i'm trying to do is search through a text file and get it to list all the punctuation characters in it. but it doesnt do it exactly how i want it to. its seems as if it is goes over one line and lists the characters and then it starts over again on the next line and then adds anything to the hash. i want it to only print out the amount of times a punctuation character comes up at the end...
#!usr/local/bin/perl print ("Enter filename to search to punctuation characters: "); $path=<STDIN>; print ("\n"); open(DATA, "<$path") || die "Couldn't open $path for reading: $!\n"; while (<DATA>) { while (s/([\041-\057]|[\72-\100]|[\133-\140]|[\173-\176])(.*)/$2/) + #ASCII octet punctuation + #characters { $char = $1; $wordHash{$char}++; } while ( ($punctuation, $count) = each(%wordHash) ) { $wordArray[$i] = "$punctuation\t$count"; $i++; print ("$punctuation\t$count\n"); } }
i get the right output, just i get it over and over again for some characters.. i dont know why! for example if i put in the filename as "wordHash.pl" which is the actual file (code) above it prints out this:
/ 3 # 1 ! 1 / 3 # 1 ! 1 / 3 : 1 # 1 " 2 ) 1 ( 1 ; 1 ! 1 $ 1 / 3 = 1 : 1 # 1 > 1 " 2 ) 1 < 1 ( 1 ; 2 ! 1 $ 1 \ 1 / 3 = 1 : 1 # 1 > 1 " 4 ) 2 < 1 ( 2 ; 3 ! 1 $ 4 \ 2 / 3 | 2 = 1 : 2 # 1 , 1 > 1 " 8 ) 3 ' 1 < 2 ( 3 ; 4 ! 2 $ 4 \ 2 / 3 | 2 = 1 : 2 # 1 , 1 > 2 " 8 ) 4 ' 1 < 3 ( 4 ; 4 ! 2 $ 4 \ 2 / 3 = 1 : 2 , 1 " 8 < 3 ; 4 ! 2 | 2 { 1 # 1 > 2 ) 4 ' 1 ( 4 $ 5 \ 10 / 6 = 1 : 2 * 1 , 1 - 4 " 8 . 1 [ 4 < 3 ; 4 ! 2 ] 4 | 5 { 1 # 1 > 2 ) 7 ' 1 ( 7 $ 5 \ 10 / 6 = 1 : 2 * 1 , 1 - 4 " 8 . 1 [ 4 < 3 ; 4 ! 2 ] 4 | 5 { 2 # 1 > 2 ) 7 ' 1 ( 7 $ 7 \ 10 / 6 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 > 2 + 4 ) 11 ' 1 } 3 ( 11 $ 18 \ 13 / 6 = 4 : 2 * 1 , 2 - 4 " 12 . 1 [ 5 < 3 ; 9 ! 2 ] 5 | 5 { 4 % 1 # 1 > 2 + 4 ) 11 ' 1 } 4 ( 11
please help... <EDIT> sorted it, sorry. i had the second while loop within the first. when i put it outside of the 1st loop it worked! </EDIT>

20060130 Janitored by Corion: Added readmore tags


In reply to punctuation search... using ascii by Yoda_Oz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.