better has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
my script, written at the bottom of this post, works, if I read a text file, which I have created before with this script:my $out = './data/IDs_created.txt'; open (OUT, ">>$out"); print "Enter ID: \n"; while (<>) { print OUT "$_"; } close OUT;
But the script doesn't work, if I use a text file instead, which I parsed from an excel csv file with this script:
use warnings; use Text::CSV; use Encode; $file = './data/IDS.csv'; #Default #Parsing CSV my $csv = Text::CSV ->new ({binary =>1, eol => $/}); open (CSV, '<:encoding(utf8)', $file) or die "Cannot open $file: $!\n" +; open (OUT, '>:encoding(utf8)', './data/IDs_created.txt') or die "Kann +Datei nicht öffnen: $!\n"; while (my $line = <CSV>) { chomp $line; if ($csv->parse($line)) { my @fields = $csv->fields (); chomp (@fields); print OUT "@fields\n"; } else { warn "Line could not be parsed: $line\n"; } } print "CSV parsed and saved as text file: IDs_created.txt!"; close CSV; close OUT;
And here is the main script: it searches and copies files from one directory into another:
#!/usr/bin/perl # #Script allows to search and copy files #Thanks to Anonymous Monk #Works only with PERL created text file!!! # #tested: --ok! use strict; use warnings; use autodie; use File::Find::Rule; use File::Slurp; use File::Basename; use File::Copy; my $startdir = shift or die Usage(); my $dirTarget = '/cygdrive/d/tmp/'; my $fnames = join '|', map quotemeta, read_file('./data/IDs_created.tx +t' , qw/ chomp 1 /); my @fnames = find( file => name => qr{$fnames}, in => $startdir ); copy ($_, $dirTarget.basename ("$_")) for (@fnames);
Anonymous monk pointed out, that it might be a problem with encoding. But after hours of reading and unsuccessfully trying, I would appreciate any help on this question.
better
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Read text file - Encoding problem?
by Kenosis (Priest) on Mar 17, 2013 at 02:05 UTC | |
by better (Acolyte) on Mar 17, 2013 at 11:32 UTC | |
|
Re: Read text file - Encoding problem?
by McA (Priest) on Mar 17, 2013 at 02:10 UTC | |
by better (Acolyte) on Mar 17, 2013 at 10:53 UTC | |
by better (Acolyte) on Mar 17, 2013 at 12:18 UTC | |
by poj (Abbot) on Mar 17, 2013 at 12:50 UTC | |
by better (Acolyte) on Mar 17, 2013 at 15:14 UTC |