<INPUT>

Discussion in 'Perl Misc' started by Gabriel, Mar 4, 2007.

  1. Gabriel

    Gabriel Guest

    How can I do a program that receive a file or any string, and count its
    \t, \n or anything that I want?

    perl miprogram jjjj.txt
    \n ---> 12
    \t ---> 7

    or

    perl miprogram bla blablba bal balb balba
    \n ---> 1
    \t ---> 0


    Thanks.
     
    Gabriel, Mar 4, 2007
    #1
    1. Advertising

  2. Gabriel

    -berlin.de Guest

    Gabriel <> wrote in comp.lang.perl.misc:
    > How can I do a program that receive a file or any string, and count its
    > \t, \n or anything that I want?
    >
    > perl miprogram jjjj.txt
    > \n ---> 12
    > \t ---> 7
    >
    > or
    >
    > perl miprogram bla blablba bal balb balba
    > \n ---> 1
    > \t ---> 0


    How would your program know that the first text is a file name and
    the second a literal text to run the count on? You'll have to specify
    that somehow.

    Apart from that, Perl's tool for character counting id tr///. Look it
    up in perlop.

    Anno
     
    -berlin.de, Mar 4, 2007
    #2
    1. Advertising

  3. Gabriel

    Brian Wakem Guest

    Gabriel wrote:

    > How can I do a program that receive a file or any string, and count its
    > \t, \n or anything that I want?
    >
    > perl miprogram jjjj.txt
    > \n ---> 12
    > \t ---> 7
    >
    > or
    >
    > perl miprogram bla blablba bal balb balba
    > \n ---> 1
    > \t ---> 0



    perldoc -q count



    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Mar 4, 2007
    #3
  4. Gabriel

    Mirco Wahab Guest

    Gabriel wrote:
    > How can I do a program that receive a file or any string, and count its
    > \t, \n or anything that I want?
    >
    > perl miprogram jjjj.txt
    > \n ---> 12
    > \t ---> 7
    >
    > or
    >
    > perl miprogram bla blablba bal balb balba
    > \n ---> 1
    > \t ---> 0
    >


    In addition to the correct ways - which were
    already mentioned, you could always use
    someting 'traditional' like (under *n*x) -
    - count anything (eg. the string "XYZ") that
    is in file "file.dat":

    $ perl -0777 -ne 'print 0+@{[/XYZ/g]}' file.dat

    or, count something that comes on a
    string, eg. the "\n":

    $ echo 'some \
    text \
    with \
    newlines \
    here ' | perl -0777 -ne 'print 0+@{[/\n/g]}'

    (returns: "5" !)

    But stick to the perldoc variants
    (which were mentioned before) if
    possible.

    Regards

    Mirco
     
    Mirco Wahab, Mar 4, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    12
    Views:
    1,714
  2. ashutosh
    Replies:
    3
    Views:
    9,055
    Andrew Thompson
    Jun 16, 2005
  3. Guy
    Replies:
    5
    Views:
    2,875
    brucie
    Dec 13, 2003
  4. cjl
    Replies:
    1
    Views:
    10,553
    Michael Winter
    Oct 31, 2005
  5. Replies:
    1
    Views:
    492
Loading...

Share This Page