Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Copying files from one file to another file

by AnomalousMonk (Archbishop)
on Feb 20, 2018 at 07:45 UTC ( [id://1209532]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Copying files from one file to another file
in thread Copying files from one file to another file

Try something like:

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: Copying files from one file to another file
by harishnv (Sexton) on Feb 20, 2018 at 07:58 UTC

    I didn't understand from here, Dd\%hash; And next line meaning? Can I know more about it?

      "...I didn't understand from here..."

      I guess it's just because some of the elder fellow monks are a lazy bunch.

      Consider this untested snippet or sketch - as you like:

      #!/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

        ... some of the elder fellow monks are a lazy bunch.

        Elderly, very lazy, and growing grouchier by the minute :)


        Give a man a fish:  <%-{-{-{-<

        the keyword "say" isn't working for me

Re^4: Copying files from one file to another file
by harishnv (Sexton) on Feb 20, 2018 at 08:34 UTC

    the path is written in terminal/ command window right? " code " should I put those apostrophe? Data::Dump::dd() is enough in the first line of the code?

      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:  <%-{-{-{-<

Re^4: Copying files from one file to another file
by harishnv (Sexton) on Feb 20, 2018 at 08:01 UTC
    Can you write comments for the code? Didn't understand dd \%hash; from there

      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'
      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:  <%-{-{-{-<

        any other solution? this is not working for me please, help me.

Re^4: Copying files from one file to another file
by harishnv (Sexton) on Feb 20, 2018 at 08:22 UTC

    Can someone help me out asap.

Re^4: Copying files from one file to another file
by harishnv (Sexton) on Feb 20, 2018 at 08:45 UTC

    not able to get the output

        I have commented below with problem and code. please check and help me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209532]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found