Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   malformated substitution attempt (http://www.velocityreviews.com/forums/t898367-malformated-substitution-attempt.html)

OllyP 06-06-2006 07:06 AM

malformated substitution attempt
 
__________________________________________________ ___________
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.

John W. Krahn 06-06-2006 08:35 AM

Re: malformated substitution attempt
 
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

Dr.Ruud 06-06-2006 08:44 AM

Re: malformated substitution attempt
 
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."



Mumia W. 06-06-2006 09:39 AM

Re: malformated substitution attempt
 
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


Mumia W. 06-06-2006 10:31 AM

Re: malformated substitution attempt
 
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.


OllyP 06-06-2006 12:29 PM

Re: malformated substitution attempt
 
* Christian Winter <thepoet_nospam@arcor.de> [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 8-) .

> 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

OllyP 06-06-2006 01:48 PM

Re: malformated substitution attempt
 
* Mumia W. <mumia.w.18.spam+nospam.usenet@earthlink.net> [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

Mumia W. 06-06-2006 06:01 PM

Re: malformated substitution attempt
 
OllyP wrote:
> * Mumia W. <mumia.w.18.spam+nospam.usenet@earthlink.net> [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


All times are GMT. The time now is 10:00 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.