Solved

Modifying a perl script

Posted on 2013-10-22
8
314 Views
Last Modified: 2013-10-25
Hi,

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  $!";
}

Open in new window


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
0
Comment
Question by:StephenMcGowan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Author Comment

by:StephenMcGowan
ID: 39594978
Is there someone able to assist me with my problem?
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39595057
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  $!";
}

Open in new window

0
 

Author Comment

by:StephenMcGowan
ID: 39595699
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:

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.

Open in new window



which are the following lines:

19: my $samp = m{($fn =~ m{_([^_]+)$})[0]}; # get samplename

28: ($r=$_) =~ s{\w+_(\w+)(?=\.csv)}{$1_$m{$1}};

Open in new window


I've attached a snapshot of the cmd errors when running.

Thanks again,

Stephen.
cmd.jpg
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 26

Expert Comment

by:wilcoxon
ID: 39595723
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:
my $samp = $m{($fn =~ m{_([^_]+)$})[0]}; # get samplename

Open in new window

0
 

Author Comment

by:StephenMcGowan
ID: 39596659
Hi Wilcoxon,

I altered the script as you had suggested:

Modded script
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,
0
 

Author Comment

by:StephenMcGowan
ID: 39596750
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).

Output table
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.
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 39598404
The existing code made some assumptions that I assumed were valid that I don't think are.  Try this slightly modified code - it should fix the issues.  If you are still getting errors then can you provide the filename and text of the line where they are occurring?
#!/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{(?:\A|_)([^_]+)$})[0]}; # get samplename
    # if this works for adding the actual filename, it should work for
    ## adding filename
    # add samplename label or actual samplename
    $\=/^"?\s*mass/?",filename,samplename$/": ",$fn,$samp$/";
    print;
}
for( <*.csv> ){
  my $r;
  ($r=$_) =~ s{(?:\A|\w+_)([^_]+)\.csv$}{$1_$m{$1}};
  rename $_,$r or warn " rename $_,$r  $!";
}

Open in new window

0
 

Author Closing Comment

by:StephenMcGowan
ID: 39599891
Really well answered!

Thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question