http://www.perlmonks.org?node_id=1034429

tbone654 has asked for the wisdom of the Perl Monks concerning the following question:

I can do this:
$ perl -e 'print chr(219);'


but I can't seem to do this:
$ cat hitters | perl -e 'while(<>){print substr($_,index($_,chr(219)));}'

from this: hitters file
2BMartin Prado▒(R)
LFJason Kubel▒(L)
CMiguel Montero▒(L)
CFA.J. Pollock▒(R)
PPatrick Corbin▒(L)
CF▒Michael Bourn▒(L)
2B▒Jason Kipnis▒(L)
SS▒Asdrubal Cabrera▒(S)
1B▒Nick Swisher▒(S)
DH▒Carlos Santana▒(S)

can someone give me a hint please? thank you...
BTW it works if I do:
$ cat hitters | perl -e 'while(<>){print substr($_,index($_,Pollo));}'
so I can search by text, but just don't know how to search on that character...

Replies are listed 'Best First'.
Re: search for chr() fails
by Anonymous Monk on May 21, 2013 at 00:40 UTC
      OQ91@BBCPC36662 /cygdrive/c/package $ perl -MData::Dump -MFile::Slurp -e " dd scalar read_file shift, { qw +/ binmode :raw / }; " hitters > ThatFilesBytesAsPerlAsciiCode.pl <br>thanks to your code I can tell the search string I'm looking for i +s \xAO<br> OQ91@BBCPC36662 /cygdrive/c/package $ cat ThatFilesBytesAsPerlAsciiCode.pl "3B\xA0Aramis Ramirez\xA0(R)\nC\xA0Jonathan Lucroy\xA0(R)\nCF\xA0Carlo +s Gomez\xA0(R)\n2B\xA0Rickie Weeks\xA0(R)\n1B\xA0Yuniesky Betancourt\ +xA0(R)\nP\xA0Yovani Gallardo\xA0(R)\nCF\xA0Robbie Grossman\xA0(S)\n2B +\xA0Jose Altuve\xA0(R)\nC\xA0Jason Castro\xA0(L)\nLF\xA0J.D. Martinez +\xA0(R)\n1B\xA0Chris Carter\xA0(R)\nDH\xA0Carlos Pena\xA0(L)\nRF\xA0J +immy Paredes\xA0(S)\n3B\xA0Matt Dominguez\xA0(R)\nSS\xA0Marwin Gonzal +ez\xA0(S)\nRF\xA0Eric Young Jr.\xA0(S)\nCF\xA0Dexter Fowler\xA0(S)\nL +F\xA0Carlos Gonzalez\xA0(L)\nSS\xA0Troy Tulowitzki\xA0(R)\nC\xA0Wilin + Rosario\xA0(R)\n1B\xA0Todd Helton\xA0(L)\n3B\xA0Nolan Arenado\xA0(R) +\n2B\xA0Josh Rutledge\xA0(R)\nP\xA0Jon Garland\xA0(R)\nSS\xA0Everth C +abrera\xA0(S)\nCF\xA0Will Venable\xA0(L)\n3B\xA0Chase Headley\xA0(S)\ +nLF\xA0Carlos Quentin\xA0(R)\n1B\xA0Yonder Alonso\xA0(L)\n2B\xA0Jedd +Gyorko\xA0(R)\nRF\xA0Chris Denorfia\xA0(R)\nC\xA0John Baker\xA0(L)\nP +\xA0Jason Marquis\xA0(L)\n"

      $ cat hitters | perl -ne 'print substr($_,index($_,"\xA0")+1,-5); print "\n";'
      Nolan Arenado
      Josh Rutledge
      Jon Garland
      Everth Cabrera
      Will Venable
      Chase Headley
      Carlos Quentin
      Yonder Alonso
      Jedd Gyorko
      Chris Denorfia
      John Baker
      Jason Marquis

      Now I can do something like:
      grep -f hitters bigger_file_to_search

      OQ91@BBCPC36662 /cygdrive/c/package
      $ grep -f dirty dirt | sort
      1B Adam LaRoche 2.1 40 WAS@SDP $2,700
      1B Billy Butler 2.7 39 KAN@OAK $3,600
      1B Brandon Belt 2.2 41 SFG@COL $3,100
      1B Brandon Moss 2.3 44 KAN@OAK $3,500
      1B Brett Pill 2 3 SFG@COL $2,600
      1B Chad Tracy 0.4 23 WAS@SDP $2,300
      1B Daric Barton 1.3 8 KAN@OAK $2,400
      1B Eric Hosmer 1.6 37 KAN@OAK $3,400
      1B Lance Berkman 2.8 37 DET@TEX $3,600
      1B Mark Kotsay 0.7 24 WAS@SDP $2,700
      1B Mitch Moreland 2.7 43 DET@TEX $3,300
      1B Nate Freiman 1.2 20 KAN@OAK $2,700
      1B Prince Fielder 3.4 41 DET@TEX $4,700


      THANK YOU VERY MUCH
        :) I learneded it from Tutorials Basic debugging checklist

        Here is another tip

        $ perl -F"\xA0" -lanse " print join q{ ### }, @F " hitters |tail P ### Jon Garland ### (R) SS ### Everth Cabrera ### (S) CF ### Will Venable ### (L) 3B ### Chase Headley ### (S) LF ### Carlos Quentin ### (R) 1B ### Yonder Alonso ### (L) 2B ### Jedd Gyorko ### (R) RF ### Chris Denorfia ### (R) C ### John Baker ### (L) P ### Jason Marquis ### (L)
Re: search for chr() fails
by LanX (Saint) on May 21, 2013 at 00:08 UTC
    you changed your code in the meantime from

    perl -e 'print substr($_,index($_,chr(219)));'

    to a while loop...

    Try to use the options "-ne" to loop over the file

    perl -ne 'print substr($_,index($_,chr(219)));'

    see perlrun

    IMHO it still doesn't do anything useful.

    I doubt you will ever explain what it was supposed to do.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I just want to return the position where it finds the character, whatever it's called... then I can print the text between the two instances of the character...
        > then I can print the text between the two instances of the character...

        many of your example lines have only one instance of that character.

        Otherwise you could just use something like  print ((split /\xA0/,$_)[1])

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        update

        corrected escape code