Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: output using map

by Marshall (Canon)
on Mar 06, 2020 at 23:12 UTC ( [id://11113921]=note: print w/replies, xml ) Need Help??


in reply to output using map

I am not sure what you are trying to accomplish with the unpack A* code? I show a modified version of your code below. It does sound like "diff" or other system utility would be just as good at this? I am on Windows, but my editor has a "diff" button for 2 different editor windows (a common feature of a program editor).

I didn't test this and I'm sure there is bound to be some mistake somewhere, but be that as it may, consider this:

#!/usr/local/bin/perl use strict; use warnings; my $a_file; # you need an initial value for these names! my $b_file; # in case the if statement doesn't work # quit unless we have the correct number of command-line args if (@ARGV != 5) { print "\nUsage: $0 new_filename orig_filename domain new_tools ori +g_tools\n"; exit(-1); } # args my ( $new_filename, $orig_filename, $domain, $new_tools, $orig_tools ) = @ARGV; my $psconfig = "psconfig.sh"; if ($new_filename eq $psconfig) { $a_file = "/directory/$new_filename" ; $b_file = "/directory/$orig_filename" ; } open my $a_fh, '<', $a_file or die "file error $a_file: $!"; open my $b_fh, '<', $b_file or die "file error $b_file: $!"; my %first_file = map { chomp; $_ => 1}<$a_fh>; my %second_file = map { chomp; $_ => 1}<$b_fh>; print "\n--------------------------------------------\n"; print "Output from $new_tools $domain $new_filename\n"; print "--------------------------------------------\n"; foreach (keys %first_file) { print "$_\n" unless exists $second_file{$_}; } print "\n--------------------------------------------\n"; print "Output from $orig_tools $domain $orig_filename\n"; print "--------------------------------------------\n"; foreach (keys %second_file) { print "$_\n" unless exists $first_file{$_}; }

Replies are listed 'Best First'.
Re^2: output using map
by AnomalousMonk (Archbishop) on Mar 07, 2020 at 00:46 UTC
    I am not sure what you are trying to accomplish with the unpack A* code?

    I think you're right that unpack is intended to chomp the newline (and maybe all whitespace?) from the end of each line.

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = qq{abc defg hi \t \n}; my $t = unpack 'A*', $s; dd $t; " "abc defg hi"
    (It's also unclear why chomp-ing would be necessary since lines are print-ed with a newline appended; no newline is appended in the OPed code, but maybe that potential problem was obscured by the fact that nothing was ever printed!)

    Note that the keys (lines) of each file will be printed in random order.


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

      I think you are correct about the intent of unpack. However this is a bad way to do it, if that is the intent. There might also be some issue related to UTF-8 filenames? But again this is not the "right way".

      I chomped the input lines and added back in a platform specific line ending in the print because in general, I don't store line endings in data structures because I strive for multi-platform code.

      Yes, foreach (keys %first_file) {} could be: foreach (sort keys %first_file) {}

        The sorting of keys (or, lines) does not help as the output would be out of sync from the order in a file. OP's code (after rewinding the file handle) would have printed the difference in lines in the same order as the lines appear in a file.

        Use of hash in OP was a handy way to keep track of lines seen. I think to keep the order of lines printed was the reason that coding_new opened the same files twice. I personally would have generated the hash with line as key & $. as value. To print, then I would have sorted on the hash value. I personally would have used Algorithm::Diff.

Re^2: output using map
by coding_new (Sexton) on Mar 09, 2020 at 13:19 UTC

    Thank You. I guess I was overthinking what I wanted to do which was to get the diff between both files output. I modified my code according to the changes you had posted and it is working as I expected.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-25 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found