Thanks a lot graff for the help!
1. file encoding is : utf-8
2. How to open zipped file with encoding?
I've written my code, review it & let me know my mistakes.
1. I've read cat file into %catHash
2. opened the main file from which I've to extract line if exists in cat file.
3. if $line
17 doesn't have value, $line
14 value to be used; if both don't have, then read next line. Am I right below?
4. $line
17 & $line
14 in main file, some have leading 0's but not in that value in cat file. for example: cat file value: 101010100 & main file has: 0101010100
What do you suggest to handle this situation. I've tried replacing these leading 0's.
I'm new to perl & programming both, please help.
#!/usr/bin/perl
use strict;
my $f = "vc_20151003_1.zip";
my $cat = "5426085.csv";
my $file = $f;
my $file =~ s/.zip/.csv/;
## Read the cat file into hash
%catHash = ();
open(CAT, $cat) || die "$cat couldn't be opened\n";
while(<CAT>) {
@line = split /,/;
$catHash{$line[1]}++;
}
close(CAT);
open(OUT, ">".$file) || "$file couln't be opened for write\n";
open(IN,sprintf("zcat %s |", $f)) || die "Could not open pipe for $f
+: $!";
while(<IN>) {
chomp;
my @line = split /\t/;
$line[17] =~ s/^0+//;
$line[14] =~ s/^0+//;
if(exists($catHash{$line[17]}) || exists($catHash{$line[14]}))
+ {
print OUT join("\t", @line) . "\n";
}
else { next; }
}
close(IN);
close(OUT);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.