Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting a new value of $1 1

Status
Not open for further replies.

vasah20

Programmer
Feb 16, 2001
559
0
0
US
I'm a perl newbie, so i have a question.

given the following code, how can I get the value of the second regex?

Code:
	foreach my $line(@file) {
		chomp($line);
		$line =~ /\$#(\@)(\w+)#\$/g;
		print "$2\n";

		$line =~ /\$#(\?)(\w+)#$/g;
		print "$2\n";
	}

It's possible that one, both, or none of the matches can occur on a single line.

Any suggestions on how to handle this would be great.

Thanks-

 
Don't rely on the $n variables to record the captured values. Use the return value of the expression in list context to record what was captured in the parentheses.
Code:
foreach my $line(@file) {
        chomp($line);
        my @ary1 = $line =~ /\$#(\@)(\w+)#\$/g;
        print "$ary1[1]\n";

        my @ary2 = $line =~ /\$#(\?)(\w+)#$/g;
        print "$ary2[1]\n";
    }
--jim
 
Question 2:

I can't get the function that the code snippet sits in to work. The recursion is breaking on me, as I can't get the $line to replace inside the if clause.

Code:
sub parseFile {
	my ($page, $atTxt, $fullLine) = ("","","");
	my @file = @_;

	foreach my $line(@file) {
		chomp($line);
		my @ary1 = $line =~ /(\$#(\@)(\w+)#\$)/g;
		
		if($ary1[2]) {
			$ary1[2] .= ".html";
			if (grep /$ary1[2]/, @dirList ) { 
				print "Processing $ary1[2]\n";
				push(@files, $ary1[2]);
				
				open(FH, $dir . "/" . $ary1[2]) or die("died inside loop $!");
				my @page = <FH>;
				close FH;
				$atTxt = parseFile(@page);

			} else {
				die &quot;Could not find $ary1[2]&quot;;
			}
				$line =~ s/$ary1[0]/$atTxt/g;

		}
		

	return $page;
}

The files being parsed, as an example, look like this:
File1.txt:
The quick brown fox jumped over the $#@File2.html#$.
Every good boy $#@File3.html#$.

File2.html:
lazy dog

File3.html:
does $#@File4.html#$

File4.html:
fine

The resulting output should be:
The quick brown fox jumped over the lazy dog.
Every good boy does fine.

Let me know if more explanation's needed. TIA.

 
I've been able to pinpoint the error more, but I don't understand what's going on.

This line isn't working:
$line =~ s/$ary1[0]/$atTxt/g;

Even though
$line == $#@File2.html#$
and
$ary1[0] == $#@File2.html#$

the switch isn't happening. Any ideas?


 
The best is when I finally RTFM, isn't it?

quotemeta is my friend.

$ary1[0] = quotemeta($ary1[0]);

Thanks all for help, hope this helps someone else.

 
If you use a regex and use the captured groups after if, ALWAYS put the regex in a conditional. You don't know if it matched or not otherwise.
Code:
    foreach my $line(@file) {
        chomp($line);
        print &quot;$2\n&quot; if($line =~ /\$#(\@)(\w+)#\$/g);

        print &quot;$2\n&quot; if($line =~ /\$#(\?)(\w+)#$/g);
    }

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
That's not true, you could check for the defined()edness of returned values, or even the $& symbol.

--jim
 
Well, $& is just evil, so we'll not get into that. If you're checking the defined()edness of one of the backreferences, might as well check the entire regex. Dunno, just my style, I guess.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top