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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reg expression not working

Status
Not open for further replies.

residence

Technical User
Feb 20, 2006
2
GB


I want an output of my file where I need to substitute all tabs with spaces and put \a at the beginning of each line. Please advise
what I am doing wrong with the last two substitute attempts:


$db = 'aaa.txt';
open(DATA, "$db") or die "did not open: $!\n";
@dat = (<DATA>);
close(DATA);
open(DATA, &quot;$db&quot;) or die &quot;did not open part 2: $!\n&quot;;
foreach $line (@dat)
{
$line =~ s/main/index/g;
$line =~ s/\t/\s/g; #This is not working
$line =~ s/\a/^/g; #This is not working
print $line;
}
close(DATA);
 
On my linux box I can do:

$line =~ s/\t/ /eg; # substitute tab with a space
$line = &quot;\\a&quot;.${line}; # add \a to the beginning

Hope this gives you something to think about. ;-)
 
Try this one...

$db = 'aaa.txt';
open(DATA, &quot;$db&quot;) or die &quot;did not open: $!\n&quot;;
@dat = (<DATA>);
close(DATA);
# I assume you meant this to open to rewrite the massaged
# data too? You forgot to tag the open for output.
open(DATA, &quot;>$db&quot;) or die &quot;did not open part 2: $!\n&quot;;
foreach $line (@dat)
{
$line =~ s/main/index/g;

# I think the problem with this is that the \s is a pseudo character
# used for matching any whitespace (tab, space, newline? ). You can't
# use that as the substitution character. I'm just replacing it with
# an actual space. You could put multiple spaces if you want.
# $line =~ s/\t/\s/g; #This is not working
$line =~ s/\t/ /g;

# this one should work fine, except that you got the terms reversed :)
# and you need to escape the backslash on the substitution
# $line =~ s/\a/^/g; #This is not working
$line =~ s/^/\\a/g;

# And last but not least, if you want this to go to the output file...
print DATA $line;
}
close(DATA);
 

Thanks for all your answers.

Okay now here is my final question on this.

Using the __DATA__ how do I get this to print in reverse.
I have tried puttin the reverse function in the print statement but no luck.

[tt]
while (<DATA>)
{
$_ =~ s/John/Jack/g;
$_ =~ s/\t/ /g;
$_ =~ s/^/\a/;
$_ =~ s/^/\\a/;
print;
}
exit 0;
close(DATA);




__DATA__
dafaf
John

John spa
tab
aaaaa[/tt]
 
The code below works.
I got rid of the &quot;$_ =~&quot; on the substitutions. It's
perfectly legal, but not needed in this case. $_ is
assumed to be the target of the substitution.
I took out the line that said &quot;s/^/\a/&quot;.
Your &quot;exit 0&quot; statement came before &quot;close(<DATA>)&quot;.
I moved it after.

It seems like saying &quot;print reverse $_&quot; or
even just &quot;print reverse&quot; should do it, but it doesn't.
(For reasons not clear to me.) Apparently you need
to use the reverse function to put what you want
reversed into another variable in order to get it to
work, in this case.

Here's your modified code:

while (<DATA>)
{
s/John/Jack/g;
s/\t/ /g;
s/^/\\a/;
$line = reverse;
print $line;
}

close(DATA);
exit 0;
 
The reverse() function needs a list (ie an Array) to be passed to it. Not a scalar.

This is just a thought, but maybe this would work:
Code:
s/\t/ /g and print &quot;\\a$_&quot; for reverse(<DATA>);
It's untested. Hope it works/helps.

--jim
 
To print out every line, even lines without a '\t' in them, change the 'and' to a ','
Code:
s/\t/ /g, s/John/Jack/g, print &quot;\\a$_&quot; for reverse(<DATA>);

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top