Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > malformated substitution attempt

Reply
Thread Tools

malformated substitution attempt

 
 
OllyP
Guest
Posts: n/a
 
      06-06-2006
__________________________________________________ ___________
Tue Jun 06 00:43:59 CDT 2006

I am not a programmer or an IT guy, but have an interest in learning
Perl. I am attempting to take a list of filepaths and rework them into
an html index so as to click them into a browser easily.
Perl, v5.8.3 Linux

The list format is:
/home/olly/aasys/perl/sandtr.html
(and on etc, for 80/90 files)

I ran:


#!/usr/bin/perl
#use warnings;
#use strict;
#use diagnostics;

use Tie::File;

tie @hot, 'Tie::File', "dead.html" or die;


for (@hot) {
s/^/<a href="file:/g;
}

for (@hot) {
s/$/">X<\/a><br>/g;
}

# At this point each line in the whole list appears similar to this:
# <a href="file:/home/olly/aasys/perl/sandtr.html">X</a><br>
# This is exactly what I want, and the X is clickable in the browser.

# However I want the filename to display instead of the X.

for (@hot =~ m/perl\/.+html/){
s/\X/\1/g;
}

# It does not displace the X with the filename.

print \1, "\n";
untie @hot, "\n";
----cut----

I have tried $& , $1 etc, but can't find the filename
The best I can do is: SCALAR(0x8186eec), however it does not replace X.

The filename will appear with the match above if I do:

-----------
my $string="<a href=\"file:/home/olly/aasys/perl/sandtr.html\">X</a><br>";
if($string =~ m/perl\/.+html/)
{s/\X/$&/};
print $&, "\n";
print $string. "\n";
#Ollynotes: this works to grab $&, but the string is unchanged?
return:

perl/sandtr.html <-- This is $& as I wanted.
<a href="file:/home/olly/aasys/perl/sandtr.html">X</a><br>
But no change .................................. ^^ here.
--------
Obviously I am violating some basic principals but can't find it in the
reams of documentation I have. I do find simple substitution aplenty
but nothing about picking up a portion and tacking it back.

Where would I look for this type of substitution?
Is there a better procedure to approach my project?
Should I throw in the towel and become a street musician?

All replies will be thoroughly read and saved.

Thank you

--
Olly P On the Gulf of Mexico USA.
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      06-06-2006
OllyP wrote:
>
> I am not a programmer or an IT guy, but have an interest in learning
> Perl. I am attempting to take a list of filepaths and rework them into
> an html index so as to click them into a browser easily.
> Perl, v5.8.3 Linux
>
> The list format is:
> /home/olly/aasys/perl/sandtr.html
> (and on etc, for 80/90 files)
>
> I ran:
>
>
> #!/usr/bin/perl
> #use warnings;
> #use strict;


You should uncomment the previous two lines.

> #use diagnostics;
>
> use Tie::File;
>
> tie @hot, 'Tie::File', "dead.html" or die;
>
>
> for (@hot) {
> s/^/<a href="file:/g;
> }
>
> for (@hot) {
> s/$/">X<\/a><br>/g;
> }
>
> # At this point each line in the whole list appears similar to this:
> # <a href="file:/home/olly/aasys/perl/sandtr.html">X</a><br>
> # This is exactly what I want, and the X is clickable in the browser.
>
> # However I want the filename to display instead of the X.


Something like this should work (UNTESTED):

#!/usr/bin/perl
use warnings;
use strict;

use Tie::File;
use File::Basename;

tie my @hot, 'Tie::File', 'dead.html' or die "Cannot open 'dead.html' $!";

for my $path ( @hot ) {
next if /^<a href=/; # skip if already converted.
my $file = basename $path;
$path = qq[<a href="file:$path">$file</a><br>];
}

__END__



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      06-06-2006
OllyP schreef:


> I [...] have an interest in learning Perl.
> [...]
> #use warnings;
> #use strict;
> #use diagnostics;


Start by removing the first character of each of those 3 lines.

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      06-06-2006
OllyP wrote:
> __________________________________________________ ___________
> Tue Jun 06 00:43:59 CDT 2006
>
> I am not a programmer or an IT guy, but have an interest in learning
> Perl. I am attempting to take a list of filepaths and rework them into
> an html index so as to click them into a browser easily.
> Perl, v5.8.3 Linux
>
> The list format is:
> /home/olly/aasys/perl/sandtr.html
> (and on etc, for 80/90 files)
>
> I ran:
>
>
> #!/usr/bin/perl
> #use warnings;
> #use strict;
> #use diagnostics;
>
> use Tie::File;
> [...]


IMO, Tie::File complicates matters. Try something like this:

use strict;
use warnings;
use File::Basename qw(basename);
use File::Slurp qw(read_file write_file);
use English;

$ARG = read_file 'file-list.txt';
s/^.*$/<a href="$MATCH">@{[ basename $MATCH ]}<\/a><br>/mg;
write_file 'file-list.html', $ARG;


__END__

The 'm' option to the s/// operator allows it to match multiple lines in
a single string. See "man perlop."

HTH

 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      06-06-2006
Mumia W. wrote:
> OllyP wrote:
>> __________________________________________________ ___________
>> Tue Jun 06 00:43:59 CDT 2006
>>
>> I am not a programmer or an IT guy, but have an interest in learning
>> Perl. I am attempting to take a list of filepaths and rework them into
>> an html index so as to click them into a browser easily.

>
> $ARG = read_file 'file-list.txt';
> s/^.*$/<a href="$MATCH">@{[ basename $MATCH ]}<\/a><br>/mg;
> write_file 'file-list.html', $ARG;
>


Oops, I forgot that the source file was HTML with some stuff in it that
he wanted to keep. That substitution should be changed to this:

s/^[\/.\w\d-]+$/<a href="$MATCH">@{[ basename $MATCH ]}<\/a><br>/mg;

I was using English and File::Slurp in the code above.

 
Reply With Quote
 
OllyP
Guest
Posts: n/a
 
      06-06-2006
* Christian Winter <(E-Mail Removed)> [2006-06-06]:
>[..msg snipped..]
> Just do it all in one iteration.
>

__________________________________________________ ___________
Tue Jun 06 05:47:03 CDT 2006

Thank you Chris, John, and Dr Ruud for looking at my post and
commenting.

> for( @hot )
> {
> s/(.*)(perl.*)/<a href="file:$1$2">$2</a>/;
> }
>
> This should produce the required output. Note the $1 and $2
> variables, that refer to the capturing parenthesises inside
> the match (see "perldoc perlvar" and "perldoc perlre" for
> details.
>

I can understand the second part easy enough Chris, assuming you omitted
the <br> unintentionally but the first part I will have to hit those doc
pages on, as it looks like it would match the whole line. Keeping in mind
that I know very little in this area .

> If you want to do the insertion of the filename in a different
> step, the following may help:
>
> if( m#(perl/.+html)# )
> {
> my $substitute = $1; # Just for readability
> s/X/$substitute/;
> }
>
> Note that I have replaced the pattern separator "/" in the above
> example with "#", thus ridding me of the need to escape the
> forward slash. This comes in handy when doing replacements on paths
> or matching on closing html tags.
>

This "if" will likely cure my problem, and I learn in the bargain.
I had read about the "cure the toothpick syndrome".

> [...]
>> if($string =~ m/perl\/.+html/)
>> {s/\X/$&/};

>
> That wont work. In your replacement pattern $& is already filled
> with the current match (this being "X"). And no need to escape the
> X character either.
>

I was beating a dead horse here Chris, and of course could not make it
print a substitution, the escape on the X was an act of desperation that I
added in the throes of agony while trying to understand what was going
wrong. I fully intended to remove that before posting where people would
see it arrgh. The same thing with the commented warnings/strict that
both John and DR Ruud admonished me about, I do understand the
importance of those. I let them swoosh right by when proof reading.
Sometimes blindly stabbing at what you think it might be can work, of course
never with munitions.

John wrote in part:

>#!/usr/bin/perl
>use warnings;
>use strict;


>use Tie::File;
>use File::Basename;


>tie my @hot, 'Tie::File', 'dead.html' or die "Cannot open 'dead.html' $!";


>for my $path ( @hot ) {
> next if /^<a href=/; # skip if already converted.
> my $file = basename $path;
> $path = qq[<a href="file:$path">$file</a><br>];
> }


>__END__

I am unfamiliar with Basename John so I will do some reading there, I
had ran out of ideas on places to look as there is so much documentation
thank you for pointing me toward it.

Thanks to all of you

--
Olly P
 
Reply With Quote
 
OllyP
Guest
Posts: n/a
 
      06-06-2006
* Mumia W. <(E-Mail Removed)> [2006-06-06]:
>
> IMO, Tie::File complicates matters. Try something like this:
>
> use strict;
> use warnings;
> use File::Basename qw(basename);
> use File::Slurp qw(read_file write_file);
> use English;
>
> $ARG = read_file 'file-list.txt';
> s/^.*$/<a href="$MATCH">@{[ basename $MATCH ]}<\/a><br>/mg;
> write_file 'file-list.html', $ARG;
>
>
> __END__
>
> The 'm' option to the s/// operator allows it to match multiple lines in
> a single string. See "man perlop."
>

__________________________________________________ ___________
Tue Jun 06 08:11:41 CDT 2006

I am amazed Mumia, shock and awe.

I ran your code with only changing it to my filename 'dead.html' and
using your second post line:
s/^[\/.\w\d-]+$/<a href="$MATCH">@{[ basename $MATCH ]}<\/a><br>/mg;

and it produced:
<a href="/home/olly/aasys/perl/sandtr.html">sandtr.html</a><br>
<a href="/home/olly/aasys/perl/perlreg.html">perlreg.html</a><br>
<a href="/home/olly/aasys/perl/ch07_05.html">ch07_05.html</a><br>
and on etc.

This is exactly what you said, and is what I was trying to get perfectly.

I was convinced I was only going to get the prefix and suffix as I
can't see what matches the embedded filename even after I ran the code
and observed the results? It is a complete mystery to me how it finds
that filename. Unless you are some sort of wizard? I am going to study
the basename module, perhaps the key is there? At the moment I am
expecting my LED screen to burst into blue flames at any second.

Thank you
--
Olly P USA Mississippi
 
Reply With Quote
 
Mumia W.
Guest
Posts: n/a
 
      06-06-2006
OllyP wrote:
> * Mumia W. <(E-Mail Removed)> [2006-06-06]:
>> IMO, Tie::File complicates matters. Try something like this:
>>
>> use strict;
>> use warnings;
>> use File::Basename qw(basename);
>> use File::Slurp qw(read_file write_file);
>> use English;
>>
>> $ARG = read_file 'file-list.txt';
>> s/^.*$/<a href="$MATCH">@{[ basename $MATCH ]}<\/a><br>/mg;
>> write_file 'file-list.html', $ARG;
>>
>>
>> __END__
>>
>> The 'm' option to the s/// operator allows it to match multiple lines in
>> a single string. See "man perlop."
>>

> __________________________________________________ ___________
> Tue Jun 06 08:11:41 CDT 2006
>
> I am amazed Mumia, shock and awe.
>


Thanks.

> I ran your code with only changing it to my filename 'dead.html' and
> using your second post line:
> s/^[\/.\w\d-]+$/<a href="$MATCH">@{[ basename $MATCH ]}<\/a><br>/mg;
>
> and it produced:
> <a href="/home/olly/aasys/perl/sandtr.html">sandtr.html</a><br>
> <a href="/home/olly/aasys/perl/perlreg.html">perlreg.html</a><br>
> <a href="/home/olly/aasys/perl/ch07_05.html">ch07_05.html</a><br>
> and on etc.
>
> This is exactly what you said, and is what I was trying to get perfectly.
>
> I was convinced I was only going to get the prefix and suffix as I
> can't see what matches the embedded filename even after I ran the code
> and observed the results? It is a complete mystery to me how it finds
> that filename.


I needed a regex that would only match file names--not HTML, and since
HTML tags tend to have angle brackets (<>) and quotes ('") in them, I
needed to choose a regex that didn't allow for those.

[\/.\w\d-]+ selects only strings that contain slashes, periods, word
characters, digits and dashes. Putting that regex between ^ and $
requires it to match the entire line. That should exclude HTML from the
substitution.

> Unless you are some sort of wizard? I am going to study
> the basename module, perhaps the key is there? At the moment I am
> expecting my LED screen to burst into blue flames at any second.
>
> Thank you


You're welcome.

File::Basename::basename only extracts the basename from a fully
qualified filename, e.g, /usr/local/pixmaps/firefox.png -> firefox.png
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't locate object method "first" via package "attempt" (perhaps you forgot to load "attempt"?) at .... GMI Perl Misc 3 06-19-2005 10:44 PM
Should this substitution be compilable? valentin tihomirov VHDL 12 11-30-2004 03:44 PM
adobe multiline substitution Justin Perl 0 12-08-2003 08:28 PM
Q: string substitution in a file Troll Perl 6 09-26-2003 01:50 PM
Converted to Mozilla but one thing missing - key macro/substitution no-spam Firefox 5 07-29-2003 08:07 PM



Advertisments