Re^3: Copying files from one file to another file
by AnomalousMonk (Archbishop) on Feb 20, 2018 at 07:45 UTC
|
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le
"my @lines = (
'foo: 12 : //comment twelve ',
'bar: 13 : //comment thirteen',
'foo: 5 : //comment five',
);
;;
my %hash;
for my $line (@lines) {
my ($k, $v) = split qr{ : \s+ }xms, $line, 2;
push @{ $hash{$k} }, $v;
}
dd \%hash;
"
{
bar => ["13 : //comment thirteen"],
foo => ["12 : //comment twelve ", "5 : //comment five"],
}
push-ing to an @{ ... } array referenced by a hash key $hash{$k} autovivifies an array reference if it did not already exist. See "autovivification" in perlglossary and here.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dump;
use feature qw (say);
my %HoA = (
bar => ["13 : //comment thirteen"],
foo => [ "12 : //comment twelve ", "5 : //comment five" ],
);
dd \%HoA;
say qq(\n--\n);
# like in "Perl Programming" AKA "Camel Book" chapter 9...
for my $key ( keys %HoA ) {
say qq($key: );
for my $item ( 0 .. $#{ $HoA{$key} } ) {
say qq( $item = $HoA{$key}[$item]);
}
print qq(\n);
}
__END__
Best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|
| [reply] |
|
|
the path is written in terminal/ command window right?
Yes.
" code " should I put those apostrophe?
No. The double-quotes (not apostrophes) are only needed on the command line for specifying source code for the -e command-line switch. See perlrun. The command-line format is only my personal way of presenting example code. What is found inside the " code " double-quotes (but not the double-quotes themselves!) is the source code you can put into a regular Perl .pl source code file to be run from the command line in the usual way (assuming Windows):
c:\yourprompt>perl your-source-code-file.pl
Data::Dump::dd() is enough in the first line of the code?
This is what I would put on the first few lines of my source file:
use warnings; # always
use strict; # always
use Data::Dump qw(dd); # if you want to use dd()
... # the rest of your source code
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
|
Can you write comments for the code?
Didn't understand dd \%hash; from there
| [reply] |
|
|
dd() is a function exported by Data::Dump. Since %hash as argument to dd would be expanded into a flat list, a reference to %hash is passed to dd. That's what the backslash does, it is the reference operator. See perlref.
The next lines are sample output produced by dd.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [d/l] [select] |
|
|
dd \%hash;
This statement "dumps" data from a data structure. The data structure must be passed to dd as a reference, hence \%hash takes a reference to the hash. See Data::Dump::dd(). (Note: There are many data dumpers. I like Data::Dump, but it's not core. The Data::Dumper module comes as a part of the core Perl "standard" installation.)
{
bar => ["13 : //comment thirteen"],
foo => ["12 : //comment twelve ", "5 : //comment five"],
}
This is the output of the dd \%hash; statement. It appears immediately after the
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le " ... "
command line invocation used to execute the example code.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
|
|
|
|
|
| [reply] |
|
|
| [reply] |
|
|
| [reply] |
|
|
|
|
Re^3: Copying files from one file to another file
by poj (Abbot) on Feb 20, 2018 at 07:54 UTC
|
$hash{$key} = $hash{$key} . $line;
or simply
$hash{$key} .= $line;
poj
| [reply] [d/l] [select] |