Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Comparision Utility PERL

by vighneshmufc (Acolyte)
on Feb 09, 2018 at 10:41 UTC ( #1208804=note: print w/replies, xml ) Need Help??


in reply to Re: Comparision Utility PERL
in thread Comparision Utility PERL

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 :)

Replies are listed 'Best First'.
Re^3: Comparision Utility PERL
by vighneshmufc (Acolyte) on Feb 09, 2018 at 10:44 UTC
    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 filename.pl file1 file2 col1fromfile1 col2fromfile2
      Ok. In Perl, the command line arguments are passed into the program as a pre-declared array, @ARGV.
      So your filename.pl 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

        The [1] in line 12 of Marshall's post refers to the second column in file1 that you want to extract into the hash keys. The first column would be [0]

         my $col2 = (split /\|/, $_)[1];

        perhaps this is easier to understand

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; #@ARGV = ('file1.txt','file2.txt','file3.txt',2,3); my ($file1, $file2, $file3, $file1col,$file2col) = @ARGV; my %hash = (); open my $fh1,'<', $file1 or die "Could not open for read $file1 : $!"; while ( my $row = <$fh1> ) { chomp $row; next if $row =~ /^\s*$/; # extract the selected column only my @fields = split /\|/, $row ; my $key = $fields[$file1col-1]; $key =~ s/^\s+|\s+$//g;# trim spaces $hash{$key} = 1; } close $fh1; print Dumper \%hash; open my $fh2,'<', $file2 or die "Could not open for read $file2 : $!"; open my $fh3,'>', $file3 or die "Could not open for write $file3 : $!"; while ( my $row = <$fh2> ) { chomp $row; next if $row =~ /^\s*$/g; # extract the selected column only my @fields = split /\|/, $row ; my $key = $fields[$file2col-1]; $key =~ s/^\s+|\s+$//;# trim spaces if ( exists $hash{$key} ) { print "$row\n"; } } close $fh2; close $fh3;
        poj

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-02-24 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (310 votes). Check out past polls.

    Notices?