Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Count of patterns in a file without knowing pattern in advance

by NetWallah (Canon)
on Jun 11, 2019 at 03:06 UTC ( [id://11101223]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Count of patterns in a file without knowing pattern in advance
in thread Count of patterns in a file without knowing pattern in advance

There is usually a "one liner" alternative for simple requirements.

I offer:

perl -anE "$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys %h" +*YOUR-FILE*
Use single quotes if you are on Linux.

                Time is an illusion perpetrated by the manufacturers of space.

  • Comment on Re^3: Count of patterns in a file without knowing pattern in advance
  • Download Code

Replies are listed 'Best First'.
Re^4: Count of patterns in a file without knowing pattern in advance
by roho (Bishop) on Jun 14, 2019 at 13:21 UTC
    Unfortunately, this experimental feature no longer exists in perl version 5.24.1 and forward...produces the following error message:

    "Experimental keys on scalar is now forbidden at -e line 1."

    "It's not how hard you work, it's how much you get done."

      Odd - works fine for me:

      $ cat foo one two one two three one four $ perl -anE '$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys %h' + foo four appeared 1 one appeared 3 three appeared 1 two appeared 2 $ perl -v | head -3 This is perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-li +nux-gnu-thread-multi (with 62 registered patches, see perl -V for more detail) $

      Are you sure you substituted single quotes as NetWallah advised?

      perl -anE "$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys %h" *YOUR-FILE*
      Unfortunately, this experimental feature no longer exists in perl version 5.24.1 and forward...produces the following error message: "Experimental keys on scalar is now forbidden at -e line 1."

      Are you sure? I don't see that feature being used in this script, did you perhaps make a typo when copying it? (Update: Note the $h vs. %h in the final example below.)

      $ perl -v This is perl 5, version 28, subversion 1 (v5.28.1) built for x86_64-li +nux ... $ cat foo.txt one two three one five two ten one $ perl -anE '$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys %h' + foo.txt five appeared 1 one appeared 3 ten appeared 1 three appeared 1 two appeared 2 $ perl -anE '$h{$F[0]}++}{say qq|$_ appeared $h{$_}| for sort keys $h' + foo.txt Experimental keys on scalar is now forbidden at -e line 1. Type of arg 1 to keys must be hash or array (not scalar dereference) a +t -e line 1, at EOF Execution of -e aborted due to compilation errors.

Log In?
Username:
Password:

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

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

    No recent polls found