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

Re: Comparision Utility PERL

by Marshall (Abbot)
on Feb 09, 2018 at 10:32 UTC ( #1208803=note: print w/replies, xml ) Need Help??

in reply to Comparision Utility PERL

I and others have tried before to help you.
#!/usr/bin/perl use strict; use warnings; use Inline::Files; my %col2file1; #use better name! while (<FILE1>) { next if /^\s*$/; #skip blank lines my $col2 = (split /\|/, $_)[1]; $col2 =~ s/^\s*//; #delete leading spaces $col2 =~ s/\s*$//; #delete trailing spaces $col2file1{$col2} = 1; } while (<FILE2>) { next if /^\s*$/; #skip blank lines my $col3 = (split /\|/,$_)[2]; $col3 =~ s/^\s*//; #delete leading spaces $col3 =~ s/\s*$//; #delete trailing spaces print if $col2file1{$col3}; } #prints: "AE|A|D|| " __FILE1__ L| D L| C L| C __FILE2__ AE|A|D|| A|A|P| | A|A|P| | A|A|P| |

Replies are listed 'Best First'.
Re^2: Comparision Utility PERL
by vighneshmufc (Acolyte) on Feb 09, 2018 at 10:41 UTC
    yes i understood it but right now i have to split from input given from the command line. How do i modify this code with respect to that ? i am sorry for such nuisance but i am trying to learn :)
      and it is not column2 and column3 it is just for an example. The user should input any column from each file he wishes for like it has to be dynamic perl file1 file2 col1fromfile1 col2fromfile2
        Ok. In Perl, the command line arguments are passed into the program as a pre-declared array, @ARGV.
        So your program might have something like this:
        my ($file1,$file2,$col1fromfile1, $col2fromfile2) = @ARGV;
        Of course as with any user input, your program should check that the files can be opened and the 2 columns "make sense".

        Write a simple program that gets 4 command line arguments and prints them. Let us know how it goes.

        my ($file1,$file2,@cols) = @ARGV; open( my ($fh2), $file2 ) or die $!; #just the file2 operations while ( my $row = <$fh2> ) { chomp $row; #print "$row\n"; next if $row =~ /^\s*$/; $row =~ s/^\s+|\s+$//g; my (@fields) = split( /\|/, $row ); # print "$fields[0]\n "; for my $fieldnum (@cols) { if ( exists $hash{ $fields[$fieldnum] } ) { print $fh "$row\n"; next; } } }
        is this correct because i have to pass array variables in CL. i was able to print but then how do i deal with arrays

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1208803]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-07-18 04:54 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (383 votes). Check out past polls.