Re: Newbie Perl Question



Rod Burgess wrote:
I am new to the Perl world and am trying to learn it. A coworker tells me
that Perl will not work for what I am trying to do however, I think Perl
would be a great tool to use and I feel this coworker is wrong.
I have a file that contains several lines all as below:
DR03555{tab} 45600062888{tab} 00008FLAT WASHER
DR03555{tab} 228765329{tab} 00001GASKET

The meaning of the file is
DR03555 = order number
45600062888 = part number
00008 = quantity
FLAT WASHER = Description

The lines all begin with the prefex DR I would like to read this file and
produce the following output:

45600062888;8;FLAT WASHER
228765329;1;GASKET

basiclly I need a file that lists the following: Part#;Quantity;Description

Is this possible with Perl?

Yes, it certainly is. Your coworker is either wrong or obtuse (or
both). Simply read the file in, split each line on the tab, match the
quantity out of the last field, convert the quantity to numeric format,
and print the fields you care about back out, joined by the semi colon.

You should read, at least, the following documentation:
perldoc -f readline
perldoc perlsyn
perldoc -f split
perldoc -f join
perldoc perlre

But, as I'm feeling generous today (and bored), here is a sample script
to get you started:
#!/usr/bin/perl
use strict;
use warnings;

while (<DATA>) {
chomp;
my ($order, $part, $third_field) = split /\t/;
my ($quant, $desc) = ($third_field =~ /^(\d+)(.*)$/);
$quant = sprintf ("%d", $quant);
print join(";", $part, $quant, $desc), "\n";
}

__DATA__
DR03555 45600062888 00008FLAT WASHER
DR03555 228765329 00001GASKET


(be careful with the formatting of the __DATA__ section - they're tabs
in my editor, but copy and pasting to this post may have mangled them)

Paul Lalli

.



Relevant Pages

  • Newbie Perl Question
    ... I am new to the Perl world and am trying to learn it. ... would be a great tool to use and I feel this coworker is wrong. ... DR035554560006288800008FLAT WASHER ... basiclly I need a file that lists the following: ...
    (perl.beginners)
  • Newbie Perl Question
    ... I am new to the Perl world and am trying to learn it. ... would be a great tool to use and I feel this coworker is wrong. ... DR035554560006288800008FLAT WASHER ... basiclly I need a file that lists the following: ...
    (perl.beginners)
  • Re: Net::DNS
    ... function because you still use $rr within the foreach, ... > So I can find the all these object methods in the perl docs listed below? ... > the return value of 'grep'. ... > perldoc perlboot ...
    (perl.beginners)
  • Re: Posting Guidelines changes (was Re: variable interpolation of refs to anonymous subroutines)
    ... Experts at Perl or experts at posting to newsgroups? ... use warnings; # see perldoc warnings ...
    (comp.lang.perl.misc)
  • Re: Multitasking
    ... Just a comment for the archives, and to any other beginner to the Perl ... When you look at the perldoc for select, perldoc -f select, you ... select FILEHANDLE ... I just meant avoid using the "Low-level socket functions" ...
    (perl.beginners)