Hello Everyone,
I am pretty new to Perl, but have been doing some independent learning. I am working on a project in which I am trying to parse through some data. But, I have gotten stuck on what seems to be an easy problem to fix, except I don't know how! :) I am trying to use the following code, but now my user IDs actually have NA in the front of them (ie. NA12324).
How can I either: do a step before this to remove the NA, OR get this code to accept this sort of ID? I hope this makes sense...thanks in advance!
#!/usr/bin/perl
use strict;
my $inFile = 'fanca.txt';
open (IN, $inFile) or die "open $inFile: $!";
my %user;
while (my $line = <IN>) {
next unless $line =~ m{^(\S+) (\d+) (.*)};
my ($site, $userID, $data, $data2) = ($1, $2, $3, $4);
$user{$userID}{$site} = $data, $data2;
}
close(IN) or die "close $inFile: $!";
my $outfile = "parsingoutput_for_fanca.txt";
open(REPORT, ">$outfile") or die "open >$outfile: $!";
foreach my $userID (sort {$a <=> $b} keys %user) {
my %sites = %{$user{$userID}};
my $line1 = 'SITES';
my $line2 = "$userID";
while (my ($site, $data, $data2) = each %sites) {
$line1 .= ' ' x (length($line2)-length($line1));
$line2 .= ' ' x (length($line1)-length($line2));
#add on next site
$line1 .= ' '. ' ' . $site;
$line2 .= ' '. ' '. $data . ' ' . ' '. $data2;
}
print REPORT $line1 . "\n";
print REPORT $line2 . "\n";
print REPORT "\n";
}
close (REPORT) or die "close $outfile: $!";
2006-10-28 Retitled by GrandFather, as per Monastery guidelines
Original title: 'Should be easy....'
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.