bdepew has asked for the wisdom of the Perl Monks concerning the following question:
I have a pearl script that right now takes a tracking file and from our shipping software and converts it into the format we need to import into our Software. the issue is the tracking # is too long and I just found out from our carrier that we can strip the first 8 characters. Is this possible?
here is the current code ("trackingno") it the ending fieldany help would be appreciated! thanks.use Text::CSV_XS; use Date::Manip; sub trim($); # Formatting variables $quote_char = '"'; $sep_char = ','; $NumHeaderRowsToSkip = 6; #$AuctionSite_EmailBlank = "amazon|pay"; $out=$ARGV[0]; # Name output file $out =~ s/(^.*).csv/$1-FS.txt/; # open file handle for output file open OUT, ">$out" or die "Cannot open $out for write :$!"; # Remove linefeeds and build array while (<>) { chomp; push (@rows, $_); } # Init csv object $csvin = Text::CSV_XS->new({ 'quote_char' => $quote_char, 'sep_char' => $sep_char }); $csvout = Text::CSV_XS->new({ 'quote_char' => $quote_char, 'sep_char' => ",", 'always_quote' => true }); # build array of header fields $headers = $rows[0]; $csvin->parse($headers); my @headers = $csvin->fields; my @rowscolumns = map { chomp; $csvin->parse($_); [$csvin->fields]; } +@rows; # build array of hashes with column name as hash index foreach my $row(@rowscolumns) { my %hashrow; my $index = 0; foreach my $val(@$row) { $hashrow{trim($headers[$index])} = trim($val); $index++ } push @arrayhash, \%hashrow; } $arrayhash[0]->{'Reference'} = 'Reference'; $arrayhash[0]->{'trackingno'} = 'trackingno'; $arrayhash[0]->{'packwt'} = 'packwt'; $arrayhash[0]->{'shipdate'} = 'shipdate'; $arrayhash[0]->{'paytype'} = 'paytype'; # construct output csv lines my $index = 0; foreach my $row(@arrayhash) { if($index>0) { $row->{'shipdate'} = &UnixDate(&ParseDate($row->{'shipdate'})," +%m/%d/%Y"); } my @output_fields = ( $row->{'Reference'}, $row->{'trackingno'}, $row->{'packwt'}, $row->{'shipdate'}, $row->{'paytype'}, ); $csvout->combine(@output_fields); $outrow[$index] = $csvout->string; $index++; } #print result to file foreach (@outrow) { print OUT "$_\n"; } sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: need to strip the first 8 characters out of a tracking #
by runrig (Abbot) on Aug 24, 2012 at 16:32 UTC | |
|
Re: need to strip the first 8 characters out of a tracking #
by MidLifeXis (Monsignor) on Aug 24, 2012 at 16:51 UTC |