StephenMcGowan
asked on
Modifying a perl script
Hi,
I'm currently trying to modify an existing perl script:
The code uses mapcode list to rename each file:
For example;
within mapcodelist, A1 is GO1.3 (it also switches fullstops to underscores).
Therefore the file becomes A1_GO1_3.csv
The script also creates a new column and enters the filename for this column.
Firstly, the script doesn't currently create a header for this column, which I'm trying to call "Filename".
Secondly, I'd like to remove the '.csv's from the end of these filenames. Please refer to "table1.jpg" to see what I'm trying to do with this.
Finally, I'd like to make a new column (called 'Samplename') to the right of this which uses the variable which is pulled from mapcodelist.txt. (So in the case of A1, this would be GO1_3).
Therefore for file A1_GO1_3, GO1_3 would therefore be copied down in this column for each row in the table. (see "table2.jpg" for further clarification).
Any help with this would be very much appreciated.
Stephen.
mapcodelist.txt
table1.jpg
table2.jpg
I'm currently trying to modify an existing perl script:
#!/usr/bin/perl
use warnings;
use strict;
open M,"<mapcodelist.txt" or die "mapcodelist.txt $!";
my %m;
while( <M> ){
my($k,$v)=split;
$v=~s/\./_/g;
$m{$k}=$v;
}
close M;
chdir "C:/Users/Stephen/Desktop/Database_Design/" or die $!;
@ARGV=<*.csv>;
$^I=".bak";
while( <> ){
chomp;
$\=/^mass/?",filename$/": ",$ARGV$/";
print;
}
for( <*.csv> ){
my $r;
($r=$_) =~ s/\w+_(\w+)(?=\.csv)/$1_$m{$1}/;
rename $_,$r or warn " rename $_,$r $!";
}
The code uses mapcode list to rename each file:
For example;
within mapcodelist, A1 is GO1.3 (it also switches fullstops to underscores).
Therefore the file becomes A1_GO1_3.csv
The script also creates a new column and enters the filename for this column.
Firstly, the script doesn't currently create a header for this column, which I'm trying to call "Filename".
Secondly, I'd like to remove the '.csv's from the end of these filenames. Please refer to "table1.jpg" to see what I'm trying to do with this.
Finally, I'd like to make a new column (called 'Samplename') to the right of this which uses the variable which is pulled from mapcodelist.txt. (So in the case of A1, this would be GO1_3).
Therefore for file A1_GO1_3, GO1_3 would therefore be copied down in this column for each row in the table. (see "table2.jpg" for further clarification).
Any help with this would be very much appreciated.
Stephen.
mapcodelist.txt
table1.jpg
table2.jpg
Try this:
#!/usr/bin/perl
use warnings;
use strict;
open M,"<mapcodelist.txt" or die "mapcodelist.txt $!";
my %m;
while( <M> ){
my($k,$v)=split;
$v=~s/\./_/g;
$m{$k}=$v;
}
close M;
chdir "C:/Users/Stephen/Desktop/Database_Design/" or die $!;
@ARGV=<*.csv>;
$^I=".bak";
while( <> ){
chomp;
my $fn = $ARGV;
$fn =~ s{\.csv$}{}; # remove .csv from filename in file
my $samp = m{($fn =~ m{_([^_]+)$})[0]}; # get samplename
# if this works for adding the actual filename, it should work for
## adding filename
# add samplename label or actual samplename
$\=/^mass/?",filename,samplename$/": ",$fn,$samp$/";
print;
}
for( <*.csv> ){
my $r;
($r=$_) =~ s{\w+_(\w+)(?=\.csv)}{$1_$m{$1}};
rename $_,$r or warn " rename $_,$r $!";
}
ASKER
Hi wilcoxon,
Thanks a lot for getting back to me. I ran the script and it half worked (the .csv's are removed successfully).
I did however receive a lot of error messages in the command prompt, mainly along the lines of:
which are the following lines:
I've attached a snapshot of the cmd errors when running.
Thanks again,
Stephen.
cmd.jpg
Thanks a lot for getting back to me. I ran the script and it half worked (the .csv's are removed successfully).
I did however receive a lot of error messages in the command prompt, mainly along the lines of:
Use of uninitialized value $} in regexp compilation at Script.pl line 19, <> line 58158.
Use of uninitialized value in concatenation (.) or string at Script.pl line 28, <> line 58158.
which are the following lines:
19: my $samp = m{($fn =~ m{_([^_]+)$})[0]}; # get samplename
28: ($r=$_) =~ s{\w+_(\w+)(?=\.csv)}{$1_$m{$1}};
I've attached a snapshot of the cmd errors when running.
Thanks again,
Stephen.
cmd.jpg
Line 19 is screwed up (the $ got dropped from $m). Line 28 is effectively unaltered from what you posted (I switched s/// to s{}{} which are syntactically identical - feel free to change it back).
Line 19 should be:
Line 19 should be:
my $samp = $m{($fn =~ m{_([^_]+)$})[0]}; # get samplename
ASKER
Hi Wilcoxon,
I altered the script as you had suggested:
I altered the first "m" on line 19 to "$19".
I'm now receiving the following error messages:
Use of uninitialized value $samp in concatenation (.) or string at script.pl line 23, <> line 58158.
and
Use of uninitialized value in concatenation (.) or string at script.pl line 28, <> line 58158.
Any ideas?
Thanks again,
I altered the script as you had suggested:
I altered the first "m" on line 19 to "$19".
I'm now receiving the following error messages:
Use of uninitialized value $samp in concatenation (.) or string at script.pl line 23, <> line 58158.
and
Use of uninitialized value in concatenation (.) or string at script.pl line 28, <> line 58158.
Any ideas?
Thanks again,
ASKER
In fact... having looked at the data, the script seems to be doing what I wanted it to do regardless of the previously mentioned error messages in the console (see picture).
The only thing that's left to do is to make the script label columns "C" and "D" as "Filename" and "Samplename" respectively.
If the error messages in the console could be stopped as well, that would also be nice. :)
Stephen.
The only thing that's left to do is to make the script label columns "C" and "D" as "Filename" and "Samplename" respectively.
If the error messages in the console could be stopped as well, that would also be nice. :)
Stephen.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Really well answered!
Thank you!
Thank you!
ASKER