Hello,
I'm quite new to perl so this is probably a simple fix. What I am trying to do is read in data from a .csv file and create a new folder for each different value in the file (the values often repeat). What I am having trouble with is that the code is generating separate folders for cells at the end of a row. For example, if I had a row of "1,1,2,2" the code is creating 3 folders: "1", "2", and "2\n". The obvious and easy solution should be the chomp command, but I am having no luck with it. I have tried putting it before and after each operation (and even before and after all operations) in case split, shift, or push was initiating the newline somehow, but the newlines persist. I should note that in the code below I refer to $_, but in other attempts I use the relevant array (@data or @row).
#!/usr/bin/perl
$filename="p1";
#input file into array
open(plate_file,$filename.".csv") or die "Can't open: $!";
$firstline=<plate_file>; #remove first line
while(<plate_file>) {
chomp(); #remove newlines???
@row=split(/,/); #split row into array elements
shift(@row); #remove first column
push(@data,@row); #append current row to data array
}
close(plate_file);
#create file structure
mkdir($filename, 0777) || print "$!\n";
foreach(@data){
if(-d "$filename/$_"){
} else {
mkdir("$filename/$_",0777) || print "$!\n";
}
}
I also tried a regular expression, but again no success. There seemed to be many ways to do remove newlines with a regex, but the code I tried was:
while(<plate_file>) {
if(/(.*)\n/){
@row=split(/,/,$1);
shift(@row);
push(@data,@row);
}
}
As a side question, there must be a more direct way to use the $1 value from the regular expression than the if statement I used. What would be the advised way?
Thanks.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.