LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2009, 09:18 PM   #1
ifeatu
Member
 
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68

Rep: Reputation: 15
Perl RegEx'ing a series of variables


I want to run some regex on an array to remove randomly placed underscores.

here is an example of some array elements:

Code:
Array element 0 is: Jan
Array element 1 is: 08
Array element 2 is: 1991
Array element 3 is: The_beginning
Array element 4 is: Berkeley
Array element 5 is: _Side
Array element 6 is: 1
Array element 7 is: .mp3
I know I would probably run a loop and then do some regex like
s/*_*// to it

Please advise!

Thanks
 
Old 12-11-2009, 10:19 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Since you are just looking to get rid of _ you might want to use tr// instead.

map(tr/_//d, @array);

this map is the same as doing

foreach (@array) {
tr/_//d;
}

Last edited by estabroo; 12-11-2009 at 10:22 PM. Reason: foreach equiv
 
Old 12-12-2009, 06:24 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by estabroo View Post
Since you are just looking to get rid of _ you might want to use tr// instead.

map(tr/_//d, @array);

this map is the same as doing

foreach (@array) {
tr/_//d;
}
Note that $array[3] will become 'Thebeginning'.

I think that tr is the way to go, and you're giving the OP what he asked for (underscores replaced by empty string), but the OP might want to rethink what he wants because of cases like $array[3].

Last edited by Telemachos; 12-12-2009 at 07:57 AM.
 
Old 12-12-2009, 06:49 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ifeatu View Post
I know I would probably run a loop and then do some regex like
s/*_*// to it
then what is stopping you?? please also read up the Perl docs. type perldoc perl on your command line, or go straight to the web site.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl seems to think I have two INC variables. versaulis Linux - Software 3 11-18-2008 07:00 PM
Perl - using positional variables with su mr_scary Programming 1 10-02-2006 05:16 AM
How to use session variables in perl ? taqs Programming 1 09-28-2004 06:12 PM
exporting variables in Perl nbp Programming 1 11-18-2003 08:35 AM
Perl variables nbp Programming 2 11-02-2003 09:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration