How do I manipulate this string in perl?
January 9, 2007 7:06 PM   Subscribe

Help! I'm trying to write perl, but I have no idea how to write perl. I need to remove charaters from a string. Can you show me how?

So, I have a string:

$str = "ABC--D----EFGH-----IJK"

I need to get rid of all those hyphens. So that it just looks like this:

"ABCDEFGHIJK"

I know that it must be easy enough, but I haven't been able to figure it out.
posted by TurkishGolds to Computers & Internet (27 answers total) 6 users marked this as a favorite
 
Response by poster: BTW, it's to impress a girl!
posted by TurkishGolds at 7:06 PM on January 9, 2007


Best answer: $str =~ s/-//g;
posted by IshmaelGraves at 7:08 PM on January 9, 2007


Best answer: $str =~ s/\-//g;
posted by Davidicus at 7:09 PM on January 9, 2007


Best answer: $str=~s/\-//g
posted by buxtonbluecat at 7:09 PM on January 9, 2007


Response by poster: Awesome. Can anyone tell me why that works?
posted by TurkishGolds at 7:11 PM on January 9, 2007


Response by poster: I've been trying to write a loop that looks at every character in the string, and then slides everything down. duh.
posted by TurkishGolds at 7:11 PM on January 9, 2007


Awesome. Can anyone tell me why that works?

If you're on *nix, a good place to start would be:

perldoc perlre

Or you could read this
posted by chndrcks at 7:15 PM on January 9, 2007


$str =~ y/-/d; # might be faster
posted by xiojason at 7:15 PM on January 9, 2007


Best answer: s/(match string)/(replace string)/(options)
s = search and replace (as opposed to m, which would be match)
the match string is a dash. I escaped mine, as the dash is also used in the form [a-z] to match any letter a through z, but it's not needed.

the replace string is blank in this case, because you want to replace "-" with ""

the g at the end means to match globally. Without it only the first - would be eliminated
posted by Davidicus at 7:15 PM on January 9, 2007


make that:
$str =~ y/-//d; # stupid fingres
posted by xiojason at 7:16 PM on January 9, 2007


Awesome. Can anyone tell me why that works?

You can spend a lifetime asking this question about Perl.

Not to start a language-war derail, but there are easier and less infuriatingly-frustrating ways to "impress a girl" than learning Perl. Other languages have perfectly good text-manipulation functions, without producing programs that are incomprehensible even to the programmer two days later.
posted by matthewr at 7:17 PM on January 9, 2007


Response by poster: I'm not trying to learn perl to impress her, I'm just trying to modify a tiny section of code so that her data will actually mean something. Which, thanks to all of you, I just did!
posted by TurkishGolds at 7:20 PM on January 9, 2007


I'll just add that, if you're interested in learning perl, I strongly reccomend picking up 'Programing Perl', by Larry Wall, et al. It's an O'Reilly book, written by the guys who wrote the language in the first place. ( and yes, that's actually good in this case) Properly written perl can be very comprehensable years later, the language doesn't force that upon you, however, so it can take a bit more effort than other languages, but I think the payoff is worth it.
posted by Davidicus at 7:24 PM on January 9, 2007


Awesome. Can anyone tell me why that works?

Mainly because the people who created perl did so in part because they got sick of writing loops to process characters.
posted by smackfu at 7:26 PM on January 9, 2007


Python? wonderful!
#!/usr/bin/python

import os
os.system('perl -e "print qq|My first python program\n|"');


Back on topic, I definitely second the rec for Programming Perl - it's a great book, and was my kickstart into the language when I first picked it up.
posted by devbrain at 7:34 PM on January 9, 2007


I much prefer to recommend Learning Perl to new folks trying to figure out how to write perl. Programming Perl is good for someone who has basic programming skills, but Learning Perl gives a much gentler introduction to programming basics, while Programming Perl just dives right in.

That said, I have Programming Perl in my bookshelf, and I haven't looked at Learning Perl in years. So, take what I say with a grain of salt.
posted by Geckwoistmeinauto at 7:41 PM on January 9, 2007


system("echo $str|tr -d '-'");
posted by kcm at 7:56 PM on January 9, 2007


Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.
Jamie Zawinski
posted by dmd at 8:12 PM on January 9, 2007 [1 favorite]


This is one of the problems for which a regular expression really is suited, no matter what your language of choice.

That said, depending on the size of your dataset, regular expressions, especially the perl engine (and the pcre) might not be a good choice due to the fact that the engine as it exists by default wants to drop your entire string into a scalar before parsing it node by node (yes, this is a bit stupid, but it is very efficient unless someone asks for something stupid like .*). If this is the case, then you might want to look at what the bioperl people are doing, or think about dropping a different regex engine in (they're replacable now).
posted by mock at 9:22 PM on January 9, 2007


xkcd is reading this thread!
posted by dmd at 9:26 PM on January 9, 2007 [1 favorite]


Python pffah-- learn Ruby, and you'll really win her heart.
And none of that newfangled 'Rails' nonsense, either!
posted by blenderfish at 9:38 PM on January 9, 2007


>Not to start a language-war derail

matthewr, I don't think you know what "not" means...

>Other languages have perfectly good text-manipulation functions, without producing programs that are incomprehensible even to the programmer two days later.

That kind of "Perl is always unreadable" nonsense is like telling kids that one puff of a joint and they'll become a raving lunatic in a straightjacket.

People don't take it seriously because it's clearly not true. And you know what proves it's not true? The first three posts in this thread. They're all exactly the same and they're all correct.

The real problem is more insidious: people like xiojason who know how to do $str =~ s/-//g; but would rather show off their obscure knowledge of the tr// operator and its deprecated form.

[I was going to do that myself.]
posted by AmbroseChapel at 10:00 PM on January 9, 2007 [2 favorites]


Not having read Programming/Learning Perl, I've got nothing to say about them. I'm sure they're great. But consider also that the Perl manpages are great - I picked up Perl4 from the manpage when I was bored one weekend: they seem to be geared more to teaching you the language than acting as a terse reference.
posted by polyglot at 10:19 PM on January 9, 2007


Programming Perl really is an excellent reference, but the manpages are also good. PP is just better once you know the basics and need to learn the rest. ;)

And no, perl programs need not be unintelligible, but they can be. My first few big ones certainly were; later ones are much better in that respect, since I now know the "right" way to do things. But I can write just as convoluted code in C as I can in perl, and I can make incredibly unintelligible SQL queries as well. SQL gives you better feedback as to what's crap and what isn't though, as crap queries take a lot longer to run. ;)

In other words, any language can be abused, perl just happens to be abused a lot because it's relatively easy to learn. Sort of like BASIC and beginners writing spaghetti code with goto.
posted by wierdo at 12:02 AM on January 10, 2007


wierdo: In other words, any language can be abused, perl just happens to be abused a lot because it's relatively easy to learn. Sort of like BASIC and beginners writing spaghetti code with goto.
I've only been dabbling in Perl for about 9 months now, but the flipside of that that I've noticed a lot when researching how to solve various problems in Perl is that many Perl programmers seem to take special pride in writing code that's as brutally short (and incomprehensible to a layman) as possible.
posted by Doofus Magoo at 3:37 AM on January 10, 2007


Don't hate Python.
str_with_no_hyphens = str.replace("-", "")

posted by cmiller at 5:53 AM on January 10, 2007


Perl is for people that don't like restrictions
Python is for people that like rules

This isn't a value judgment, it's just what it is. Python is a fairly strictly "typed" language, whereas Perl has wild flexibility in how it can be written. As such, there's a lot of unreadable Perl out there. However, you can find crap code in any language. It is entirely possible to write readable Perl. I make a living doing it.

#!/usr/bin/perl
my $statement = "your favorite language sucks";
$statement =~ s/sucks/rules/;
print "$statement\n";
posted by mcstayinskool at 7:35 AM on January 10, 2007


« Older What should a company pay me to move to Burbank...   |   Good diet-friendly caffeinated drinks? Newer »
This thread is closed to new comments.