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

How to remove an apostrophe?

by sfhazel (Novice)
on Oct 12, 2005 at 18:27 UTC ( #499630=perlquestion: print w/ replies, xml ) Need Help??
sfhazel has asked for the wisdom of the Perl Monks concerning the following question:

I am using the following code to do two things. First, I split the full filename by the slash, so I can remove the directory from the name. Then, once I have the file name, I try to remove the apostrophe. Well, This is where my problem is. The apostrophe remains no matter what I do. I've even tried using split to get rid of it, but to no avail. So, my question is: Am I retarded?
@string_parts = split(/\\/,$fullName); foreach $part (@string_parts){ $newName = $part; } $newName =~ s/\'//g;

2005-10-12 Retitled by Arunbear, as per Monastery guidelines
Original title: 'Am I Retarded?'

Comment on How to remove an apostrophe?
Download Code
Re: How to remove an apostrophe?
by ikegami (Pope) on Oct 12, 2005 at 18:31 UTC

    Your code works:

    $fullName = "c:\\path\\boo's"; @string_parts = split(/\\/,$fullName); foreach $part (@string_parts){ $newName = $part; } $newName =~ s/\'//g; print("$newName\n"); __END__ output ====== boos

    Are you sure it's an apostrophe and not a (unicode?) character that looks like one? Or maybe a backtick (`)?

    By the way,
    foreach $part (@string_parts) { $newName = $part; }
    can be reduced to
    $newName = $string_parts[-1];

      It is pulled from the filename: "00_te'st.jpg"

      So, I'm sure it's not a backtick. If it is unicode, what would you suggest to fix it?

      Also, thank you for simplifying that other peice of code. I'm still in the "get the thing to work" phase of development. hehe.

        Find out what character it is using the snippet below, then use \x{####} instead of an apostrophe in the regexp, where "####" is the number printed by the snippet below).

        printf("%X\n", ord(substr($newName, 5, 1)));
Re: How to remove an apostrophe?
by Skeeve (Vicar) on Oct 12, 2005 at 18:53 UTC
    use File::Basename; ($newname= basename $fullname) =~ tr/'//d;
    untested

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%.+=%;.#_}\&"^"-+%*) +.}%:##%}={~=~:.")&e&&s""`$''`"e
Re: How to remove an apostrophe?
by liverpole (Monsignor) on Oct 12, 2005 at 19:04 UTC
    To make sure the characters in the filename are what you think they are, try something like this to decipher $newName:
    map { printf "Char(%s) = Ascii %d\n", $_, ord $_ } split //, $newName;
    For reference, you can also display an ascii chart (showing hex, decimal and the character) with:
    map {printf"%3d %2x %2s|",$_,$_,$_<32?'^'.chr$_+64:$_<127?chr$_:'^?'} +(0..127);
    which displays:
      0  0 ^@|  1  1 ^A|  2  2 ^B|  3  3 ^C|  4  4 ^D|  5  5 ^E|  6  6 ^F|  7  7 ^G|
      8  8 ^H|  9  9 ^I| 10  a ^J| 11  b ^K| 12  c ^L| 13  d ^M| 14  e ^N| 15  f ^O|
     16 10 ^P| 17 11 ^Q| 18 12 ^R| 19 13 ^S| 20 14 ^T| 21 15 ^U| 22 16 ^V| 23 17 ^W|
     24 18 ^X| 25 19 ^Y| 26 1a ^Z| 27 1b ^[| 28 1c ^\| 29 1d ^]| 30 1e ^^| 31 1f ^_|
     32 20   | 33 21  !| 34 22  "| 35 23  #| 36 24  $| 37 25  %| 38 26  &| 39 27  '|
     40 28  (| 41 29  )| 42 2a  *| 43 2b  +| 44 2c  ,| 45 2d  -| 46 2e  .| 47 2f  /|
     48 30  0| 49 31  1| 50 32  2| 51 33  3| 52 34  4| 53 35  5| 54 36  6| 55 37  7|
     56 38  8| 57 39  9| 58 3a  :| 59 3b  ;| 60 3c  <| 61 3d  =| 62 3e  >| 63 3f  ?|
     64 40  @| 65 41  A| 66 42  B| 67 43  C| 68 44  D| 69 45  E| 70 46  F| 71 47  G|
     72 48  H| 73 49  I| 74 4a  J| 75 4b  K| 76 4c  L| 77 4d  M| 78 4e  N| 79 4f  O|
     80 50  P| 81 51  Q| 82 52  R| 83 53  S| 84 54  T| 85 55  U| 86 56  V| 87 57  W|
     88 58  X| 89 59  Y| 90 5a  Z| 91 5b  [| 92 5c  \| 93 5d  ]| 94 5e  ^| 95 5f  _|
     96 60  `| 97 61  a| 98 62  b| 99 63  c|100 64  d|101 65  e|102 66  f|103 67  g|
    104 68  h|105 69  i|106 6a  j|107 6b  k|108 6c  l|109 6d  m|110 6e  n|111 6f  o|
    112 70  p|113 71  q|114 72  r|115 73  s|116 74  t|117 75  u|118 76  v|119 77  w|
    120 78  x|121 79  y|122 7a  z|123 7b  {|124 7c  ||125 7d  }|126 7e  ~|127 7f ^?|
    
    
      map {printf"%3d %2x %2s|",$_,$_,$_<32?'^'.chr$_+64:$_<127?chr$_:'^?'} +(0..127);

      Cool (and useful) one-liner, replacing man ascii!

      I added it to my ~/.bash/functions, with a few changes replace the quote chracters so it would be acceptable to bash. Also, added a newline for every 8 characters.

      function ascii { perl -e ' map { printf q{%3d %2X %2s|%s}, $_, $_, $_<32 ? q{^}.chr($_+64) : $_<127 ? chr : q{^?}, ($_+1)%8 ? q{} : $/ } 0..127 ' }

      /prakash

      PS: I tried making it a bash alias, but it was too much trouble working around all those quotes to prevent bash interpolating $_ etc!

Re: How to remove an apostrophe?
by sauoq (Abbot) on Oct 12, 2005 at 19:38 UTC
    Then, once I have the file name, I try to remove the apostrophe.

    The preferred practice when cleaning up things like filenames is to specify what you will accept rather than what you will reject. For instance, if you used a substitution like $newName =~ s/[^\w.-]//g; you may not have run into this problem at all. (Assuming that the problem really was a character in your filename that only appeared to be an apostrophe.)

    -sauoq
    "My two cents aren't worth a dime.";
    
      For completeness. sauoq is referring to the practice of whitelisting (specifying what you'll accept) vs. blacklisting (specifying what you'll reject). Whitelisting is generally considered to be preferable because the list of things you know you want is generally easier to specify than the list of things you know you don't want. This is especially true when dealing with untrusted input because you can never know about all the crazy wacked-out $h!t people are going to throw at you, maliciously or stoopidly. (Though, one can argue that stoopidity is a form of negligent maliciousness...)

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://499630]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-07-30 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (240 votes), past polls