LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 01-08-2012, 04:02 AM   #1
doughyi8u
Member
 
Registered: Apr 2010
Posts: 254

Rep: Reputation: 10
simple regex problem


I'm trying to write a Perl script that compiles any code in a directory. The thing is, I need to name these files (I'm using the name taken from the source code but w/o the trailing ".c".

Code:
foreach (@srccode) {
    $file =~  # this is where I'm at a loss
    system("gcc -o $file $_");
}
How do I get the name of the file minus the ".c" in the $file variable?
Thanks advance.
 
Old 01-08-2012, 06:27 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I think you want something like:
Code:
/(.*).c/;
$file = $1;
Probably a better way but I am a bit of a noob in Perl
 
Old 01-08-2012, 09:02 AM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
( I misread the question ... cheers, makyo )

Last edited by makyo; 01-08-2012 at 02:40 PM.
 
Old 01-08-2012, 01:05 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
There is more than one way...
Code:
map { system "gcc -o ". /(.*)\.c/ ." ". $_ } @srccode;
 
Old 01-08-2012, 07:55 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

In case you wanted to make (pun intended) it more like make:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate compile for missing executable or out-dated.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl

pl " All files:"
ls

pl " perl code:"
cat p2

rm -f hi
pl " Results, no executable:"
./p2

pl " Results, executable exists:"
./p2

sleep 1
pl " Results, source is newer than executable:"
touch hi.c
./p2

exit 0
producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
perl 5.10.0

-----
 All files:
a.c.x  hi  hi.c  p1  p2  readme.txt  s1  xxc

-----
 perl code:
#!/usr/bin/env perl

# @(#) p1	Demonstrate testing for existence, out-of-date.

@src = glob("*.c");
for $s (@src) {
  $s =~ /(.*)\.c$/;
  $file = $1;
  if ( !-f $file || ( ( stat($file) )[9] < ( stat($s) )[9] ) ) {
    print " File $file does not exist or out of date, compiling.\n";
    print " gcc -o $file $s\n";
    system("gcc -o $file $s");
  }
  else {
    print " File $file exists and is newer than source $s\n";
  }
}

-----
 Results, no executable:
 File hi does not exist or out of date, compiling.
 gcc -o hi hi.c

-----
 Results, executable exists:
 File hi exists and is newer than source hi.c

-----
 Results, source is newer than executable:
 File hi does not exist or out of date, compiling.
 gcc -o hi hi.c
Best wishes ... cheers, makyo
 
  


Reply



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
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
[SOLVED] What's Wrong with the RegEx [Simple] thund3rstruck Programming 2 04-04-2011 11:04 AM
[SOLVED] Simple regex match without replace? nrg Programming 1 11-21-2009 03:04 AM
simple regex not so simple (perl) ludeKing Programming 5 03-02-2005 02:29 AM
simple perl and regex phlx Programming 6 12-03-2004 03:01 PM

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

All times are GMT -5. The time now is 03:13 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