Click to See Complete Forum and Search --> : lc problem when working with arrays


syn
10-22-2001, 12:56 PM
the below program formats things really weird
I get an output with a blank space before each word right after the first one prints out. The lowercasing of the words in the array works but the quirky output is bothering me and I cant figure out why.
I tried printing @lcarray and that just gave me the same thing. Can anyone help?

#!/usr/bin/perl -w
use strict;

my @array;
my $lcarray;
open (FILE, "test1.txt") || die "cant open: $!";
@array = <FILE>;
$lcarray =lc ("@array\n");
print $lcarray;

output:

one
two
three
four
five

TheLinuxDuck
10-22-2001, 03:36 PM
I'm not quite getting where the problem is.. do you mean that each individual word has a space before it, on it's line? Or there is a blank line before the first word?

TheLinuxDuck
10-22-2001, 03:46 PM
Oh.. ok, I'm down.. the problem is that when you call lc with the parameter "@array", the @array is interpolated into a string, each slice being seperated by a space. So, if you have an array as

my(@array)="Bacon","Sausage","Cheese";

printing it as:

print "@array\n";

will result in

Bacon Sausage Cheese

whereas if you printed it as:

print @array, "\n";

you would get:

BaconSausageCheese

You could, instead of slurping the whole file into the array with one line, loop through it with a while, and lc each item, then push it onto the array, as:

#!/usr/bin/perl -w
use strict;

my @array;
my $lcarray;
open FILE, "test1.txt" || die "cant open: $!";

while(<FILE> ) {
push @array, lc;
}

close FILE;
print @array;


Hope that helps!

syn
10-23-2001, 06:36 PM
Dang cool! this board helps me more often than you can imagine (thanx! :-)
now all I gotta do is get the program to process as many files as are in the commandline and Ill go from there. I know its been done before, but I wanna make a wordlist helper to join wordlists and sort and delete duplicates. So sucking the entire contents into memory is gonna be a problem, so I gotta figure out how to read in each line from each file and output it to another txt file. Wish me luck!
thanx again for the help

TheLinuxDuck
10-24-2001, 09:32 AM
syn:

If you're having any troubles with the other portions of the script, I'd be happy to help as I can.

syn
10-24-2001, 04:20 PM
thanx for the offer, I might take you up on that ;-) This is a learning experience for me so I kinda want to explore all the options that I can think of before asking for help. But Im sure Ill run into something though. lol