On Dec 21, 2003, at 10:47 PM, Bill Jastram wrote:

James:

Thanks for the sample and I agree it does work.

How can I create an array of just the first names for a file?

This is what I have so far:

Let's take a look at what you have first.


You're missing two very important lines right here:

use strict;
use warnings;

These promise Perl you'll play by the good programmer rules, so it can help you find problems. That's a good deal.

open (A, "testing.txt");

Always check if an open succeeded. There are plenty of reasons it may not.


Also, you'll generally stay more sane if you use better names for file handle/variables than A.

open CONTACTS, 'testing.txt' or die "File error: $!";

@A = <A>;

my @A = <CONTACTS>;


We're reading the whole file here, but you only need one line at a time. We can do better.

foreach ($n = 0; $n<10; $n++)
{

#Split each record into its fields
$item =  $A[$n];

These two lines can be simplified:


for my $item (@A[0..9]) { # if you really only wanted the first ten lines

# or...

for my $item (@A) { # if you wanted them all

@addArray = split( "\t", $item); #Splits each line into its tab fields
$first = $addArray[0]; #Breaks down array into proper fields
$last = $addArray[1];
$add  = $addArray[2];
$city = $addArray[3];
$state = $addArray[4];
$zip = $addArray[5];

my($first, $last, $add, $city, $state, $zip) = split /\t/, $item;


printf  "\n%s %s\n%s\n%s %s %s\n", $first, $last, $add, $city, $state,
$zip;
_________________________________________________

Let's try something simpler:


#!/usr/bin/perl

# use with:
# perl this_script_name testing.txt

use strict;
use warnings;

while (<>) {      # process args line by line
        printf "\n%s %s\n%s\n%s %s %s\n", split /\t/, $_;
}

__END__

What it seems I need now is to create arrays for each of the fields, so I
can proceed to make three columns of labels.

That's a little trickier, but let's see if we can keep it pretty simple:


#!/usr/bin/perl

# use with:
# perl this_script_name testing.txt

use strict;
use warnings;

my(@col1, @col2, @col3);

my $col = 1;
while (<>) {
        if ($col == 1) { push @col1, $_ }
        elsif ($col == 2) { push @col, $_ }
        else {
                push @col3, $_;
                $col = 1;
                next;
        }
        $col++;
}

# that should load @col1, @col2 and @col3
# can you come up with an output loop for them that goes here?

__END__

Does that help you along?

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to