Hi,
I want to output the result into the file shared the same basename but
different extensions,
Below is what I have come up so far:
perl try.tex
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
my $bib_filename = "/home/lina/texmf/bibtex/bib/biophymd.bib";
my $bib_abbrev_filename ="/home/lina/texmf/bibtex/bib/biophyabbrev.bib"
open my $bib_abbrev, '<', $bib_abbrev_filename
my $bib_output, '>', "basename($ARGV[0]).bib"
### here the ARGV[0] is try.tex, so the output filename is try.bib.
my %dict;
my $tex_filename = $ARGV[0] ;
open my $file, '<', $tex_filename or die "Can't open $tex_filename:$!";
while (my $line = <$file>) {
if($line =~ m/cite\{(\S+)\}/g) {
print $1,"\n";
}
}
Thanks for your time,
Best regards,
Hi lina,
On Sun, 4 Mar 2012 00:37:58 0800
That's good so far.
You're missing a trailing semicolon (";") here.
And here.
Perl won't interpolate function calls inside double-quotes. You can do:
open my $bib_output, '>', basename($filename) . ".bib"
or die "Foo bar. $!";
You can also do "*******" (using the "Turtle operator"
mentioned here - http://www.catonmat.net/blog/secret-perl-operators/ - butthis
is clunky.
You've also missed the open and the trailing ";" again.
You're mentioning $ARGV[0] twice in this script. You should assign it to a
variable once and use it instead. Also see:
http://perl-begin.org/tutorials/bad-elements/#subroutine-arg[..]
It is possible that here ":" will be considered as part of the variable nae,
because Perl uses "::" as a namespace separator. So you should write
${tex_filename} instead to delimit it.
I also dislike calling variables "file" because it can refer to a file hanle,
a file name, the file contents or whatever.
Here it is better to do if (my ($match_substring) =~ m/cite\{(\S )\}/)) ecause
$1 can easily be clobbered (see Perl Best Practices about it). Why are you
using /g here?
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang
Beliefs are what divide people. Doubt unites them.
â=80” http://en.wikiquote.org/wiki/Peter_Ustinov
Please reply to list if it's a mailing list post - http://shlom.in/reply .
# for catfile()
use File::Spec;
# divide the input file name into its path and name,
# ignore its extension
my ( $name, $path ) = fileparse( $tex_filename, qr/\.[^\.]+$/ );
# create a new name with .bib extension
my $output_text_filename = File::Spec->catfile( $path, "$name.bib" );
# open the file
open my $bib_output, '>', $output_text_filename
or die "could not open $output_text_filename: $!\n";
# etc...
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
It's Mutual Aid, not fierce competition, that's the dominate
force of evolution. Of course, anyone who has worked in
open source already knows this.
It's global match.
$ more try.tex
bbbbb
aaaa \cite{metabolism}
ccc \cite{toxic2}
EEE \cite{toxic4}
ddd cite not
$ perl extract.pl try.tex
Useless use of a constant (>) in void context at extract.pl line 14.
Use of uninitialized value $match_substr in pattern match (m//) at
extract.pl line 24, <$texfile> line 1.
Use of uninitialized value $match_substr in pattern match (m//) at
extract.pl line 24, <$texfile> line 2.
Use of uninitialized value $match_substr in pattern match (m//) at
extract.pl line 24, <$texfile> line 3.
Use of uninitialized value $match_substr in pattern match (m//) at
extract.pl line 24, <$texfile> line 4.
Use of uninitialized value $match_substr in pattern match (m//) at
extract.pl line 24, <$texfile> line 5.
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
my $INPUTFILE = $ARGV[0];
my $bib_filename = "/home/lina/texmf/bibtex/bib/biophymd.bib";
my $bib_abbrev_filename ="/home/lina/texmf/bibtex/bib/biophyabbrev.bib";
open my $bib_abbrev, '<', $bib_abbrev_filename ;
my $bib_output, '>', basename($INPUTFILE)."bib" or die "Can't write
the output$!";
my %dict;
my $tex_filename = $INPUTFILE ;
open my $texfile, '<', $tex_filename or die "Can't open ${tex_filename}:$!;
while (my $match_substr = <$texfile>) {
if( my ($match_substr) =~ m/cite\{(\S )\}/g) {
print $match_substr,"\n";
}
}
Thanks for your help,
Best regards,
$ perl extract_v2.pl try.tex
Undefined subroutine &main::fileparse called at extract_v2.pl line 16.
#!/usr/bin/env perl
use strict;
use warnings;
use File::Spec;
my $INPUTFILE = $ARGV[0];
my $tex_filename = $INPUTFILE;
my $bib_filename = "/home/lina/texmf/bibtex/bib/biophymd.bib";
my $bib_abbrev_filename ="/home/lina/texmf/bibtex/bib/biophyabbrev.bib";
# divide the input file name into its path and name,
# ignore its extension
my ($name, $path) = fileparse($tex_filename, qr/\.[^\.] $/);
# create a new name with .bib extension
my $output_bib_filename = File::Spec->catfile($path,"name.bib");
open my $bib_output, '>', $output_bib_filename
or die "Could not open $output_bib_filename:$!\n";
open my $texfile, '<', $tex_filename or die "Can't open ${tex_filename}:$!;
while (my $line = <$texfile>) {
if( $line =~ m/cite\{(\S )\}/g) {
my $match_entry = $1 ;
print $match_entry,"\n";
}
}
Thanks,
The fileparse function is in File::Basename, so you need to add
use File::Basename;
before you can use Shawn's code.
Rob
use File::Basename;
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
It's Mutual Aid, not fierce competition, that's the dominate
force of evolution. Of course, anyone who has worked in
open source already knows this.