Re: do does not read massive containing element w/ ' sign.
by RonW (Parson) on Aug 21, 2015 at 16:04 UTC
|
do is not the best way to read your data. do treats the contents of the file it reads as Perl source code.
I would suggest using Text::CSV
| [reply] [d/l] [select] |
|
|
Thank you. But do you have other suggestions -- as i have to use diver signs in the strings -- including commas, spaces -- so, any delimiter except TAB, CR/NL. -- Meaning that there always will be a sing that needs to be prefixed w/ _\_ sign?
| [reply] |
|
|
| [reply] [d/l] |
|
|
Re: do does not read massive containing element w/ ' sign.
by 1nickt (Canon) on Aug 21, 2015 at 16:16 UTC
|
So, question is, How i should store my data regarding sign _'_ ?
Your data is already in fields and in rows, so why not make it a CSV file?
$ cat 1139428.dat
qweqwe,rtyr tyr,'asdasd,fghfghfgh
foo,bar,baz,qux
$
$ cat 1139428.pl
#! perl
use strict;
use warnings;
use Text::CSV_XS 'csv';
my $data = csv( in => '1139428.dat' );
foreach my $row ( @{ $data } ) {
foreach my $field ( @{ $row } ) {
print "$field\n";
}
}
__END__
$
$ perl 1139428.pl
qweqwe
rtyr tyr
'asdasd
fghfghfgh
foo
bar
baz
qux
$
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
|
|
I think it just makes my script more complicated at no worth: while moving by the array, i simply take by two elements, serially: 1,2 them 3,4 etc. So i have no need to organize the array as a table row/column. -- It is the first. The second is i do not go away from my problem w/ _'_ sign but simply change to another problem (w/ _,_ sing -- that i also has in my strings). So unless there is something i have missed from your suggestion, i see no reason to change things. For now i am focused on saving almost any character (in UTF-8) except TAB, CR etc and speed up my first parts of the pairs (of two elements) by writing them in the array as precompiled regexps.
| [reply] |
|
|
| [reply] [d/l] |
|
|
|
|
|
Re: do does not read massive containing element w/ ' sign.
by marinersk (Priest) on Aug 21, 2015 at 15:35 UTC
|
I am unable to reproduce your issue.
Constructing a test data file from your snippet above:
'qweqwe', 'rtyr tyr',
'\'asdasd', 'fghfghfgh'
And constructing a Perl script from your snippet:
use strict;
use warnings;
use Data::Dumper;
open my $file, '<', 'test1.dat';
my @data=do $file;
close $file;
print Dumper \@data;
exit;
__END__
Produces:
D:\PerlMonks>read1.pl
$VAR1 = [
undef
];
D:\PerlMonks>
This looks nothing like the problem you described.
I must be guessing wrong about what your script is doing. Please see How do I post a question effectively?
| [reply] [d/l] [select] |
|
|
use strict;
use warnings;
use Data::Dumper;
my @data=do 'test1.dat';
print Dumper \@data;
__END__
$VAR1 = [
'qweqwe',
'rtyr tyr',
'\'asdasd',
'fghfghfgh'
];
| [reply] [d/l] |
|
|
Excuse me, but could you please explain what is the proper way here -- as i see, you do the same i do, except you use that module Data? -- I have not hard coded the file name, nor did print it all out. So?
| [reply] |
|
|
Thank you for your answer!
I do that just like that, -- no open/close file:
my @data=do $file;
-- always worked for me until i got the string, containing _'_ sign.
| [reply] [d/l] |
|
|
#!perl
use strict;
# in1.txt
# '\'asdasd', 'fghfg, hfgh'
my @data = do 'in1.txt';
print "$_\n" for @data;
The above gives an output of
'asdasd
fghfg, hfgh
Is the problem that you want this ?
\'asdasd
fghfg, hfgh
poj | [reply] [d/l] [select] |
|
|
Re: do does not read massive containing element w/ ' sign.
by Anonymous Monk on Aug 21, 2015 at 15:38 UTC
|
First, why do you use the term "massive" instead of array?
I can't reproduce the problem you are claiming:
$ cat data.pl
'asdasd',
"'asdasd",
'\'asdasd',
"\\'asdasd",
'\\\'asdasd',
$ perl -MData::Dumper -e 'print Dumper [do "data.pl"]'
$VAR1 = [
'asdasd',
'\'asdasd',
'\'asdasd',
'\\\'asdasd',
'\\\'asdasd'
];
Which is pretty much expected given Perl's behavior in regards to quoting rules, see e.g. Quote Like Operators and Quote and Quote like Operators.
Perhaps you could describe what you are trying to achieve more exactly, with some real sample input and expected output - see How do I post a question effectively? and Basic debugging checklist | [reply] [d/l] |
|
|
1. I'm not english speaker of course, so, i supposed it being a noun. Now i will use array instead. Thank you for pointing out.
2. I do not know why it works for you. Can you equal do result to array, and print out the element, holding _'_ sign? (I'm not familiar w/ Data::Dumper) Should i pass my array through Dumper before using?
| [reply] |
|
|
nicolay, you must read! If you see a module used here that you don't know, look it up!!!! I don't know how much of the docs are in your native language, but I think your English is quite good enough to read most docs as the language is (supposed to be) clear and technical.
Data::Dumper is a very useful tool for development. Many problems can be solved by looking closely at the contents of your data structures and discovering what your data actually is. Data::Dumper does this.
You won't use Data::Dumper in production usually, so you don't need to "pass your array through it."
The way forward always starts with a minimal test.
| [reply] [d/l] |
|
|