LinuxQuestions.org
Help answer threads with 0 replies.
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 11-02-2004, 01:31 AM   #1
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Rep: Reputation: 15
perl: using 'substr' in regex


hi guys,
i wrote the following program in perl.

Code:
open(TEMP, "temp_file"); # this file is nothing but a list of filenames, one name in each line.

@arr = <TEMP>;

$temp = "@arr";

$line = "M1BRMA00"; # this line exists in "temp_file"

if ($temp =~ /substr($line, 0, 5)/)
{
    print "found";
}
else
{
    print "not found";
}
surprisingly(to me, that is) it printed "not found".

but when i assign the 'substr' part to another string, as in this prog,

Code:
open(TEMP, "temp_file"); 

@arr = <TEMP>;

$temp = "@arr";

$line = "M1BRMA00"; # this line exists in "temp_file"
$tempstr = substr($line, 0, 5);

if ($temp =~ /$tempstr/)
{
    print "found";
}
else
{
    print "not found";
}
it prints "found" which is correct. why? since 'substr' returns a subset of the target string, what is the difference between the two programs?

thanks for any help.
 
Old 11-02-2004, 01:36 AM   #2
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
the difference is in the regular expression -- check your syntax on how to construct one -- you cannot just throw code in a regex all willy-nilly like and expect it to work....

--Regular Expressions have their own syntax, which differs from that of perl!
 
Old 11-02-2004, 01:51 AM   #3
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
more:
i'm not really looking to trace through your code there, but why aren't you using a 'foreach'... seems to me you're lucky to be getting a found -- $temp = "@arr"??

maybe i just haven't written any perl in a while, but i would make it something like:

Code:
#!/usr/bin/perl
###########
## Moray ##
###########
open(TEMP, "temp_file"); 
my @lines_of_temp = <TEMP>;
close(TEMP);
$find_me = "M1BRMA00";

foreach (@lines_of_temp){
    if(m/$find_me/){
        # found
    }else{
        print "not ";
    }
    print "found\n";
}
--yup, a quick modification and run, and that there is some good code!


[edit] add a counter to that, or something to not print every line... i ran this on a 9-line file, and it printed an accurate 'found' on line 8 of the result... there will be one line printed for every line in your file without any modification...[/edit]

Last edited by secesh; 11-02-2004 at 01:54 AM.
 
Old 11-02-2004, 03:08 AM   #4
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Original Poster
Rep: Reputation: 15
thanks for ur replies secesh. yeah, the '(' and ')' is the problem, right? because it is part of regex syntax. stupid of me to ask the question in the first place. but how do i include a function in a regex? or pls tell me where i can read up on it.
thanks.
 
Old 11-02-2004, 06:52 PM   #5
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
regexes can be very complex -- i have never really gone that deep with them... start with google - there's tons of documention on them... sorry i can't help more.
 
Old 11-03-2004, 01:58 AM   #6
Scorpions4ever
LQ Newbie
 
Registered: Nov 2004
Distribution: RedHat/Fedora/Slackware/Suse
Posts: 15

Rep: Reputation: Disabled
Something like this ought to do the trick.
Code:
#!/usr/bin/perl -w

$line = "MBR100456";
$tmp = "MBR100678";
if ($tmp =~ /(?{substr($line, 0, 5);})/) {
    print "Found\n";
} else {
    print "Not found\n";
}
 
  


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
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM
perl - regex - negation beebop Programming 9 06-19-2005 08:25 PM
simple perl and regex phlx Programming 6 12-03-2004 03:01 PM
Regex help needed in perl Dr Twox Programming 2 08-07-2004 05:58 AM
perl regex question JustinHoMi Programming 5 03-20-2002 07:46 AM

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

All times are GMT -5. The time now is 11:57 AM.

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