Need help to ignore leading spaces

A

asm_toronto

I have a datafile (over which I have no control) with a lot of leading
blank spaces
in certian records, which affects my alphabetic sorts.

Can someone help me make this routine ignore leading spaces?

open (DATA, "$DataDir/offline.txt");

my @aro = map { join '|', @$_ }
sort { lc($a->[1]) cmp lc($b->[1]) }

map { [ split /\|/ ] } <DATA>;

foreach $Ad (@aro) {
@Field = split (/\|/, $Ad);
if ($Field[0] ne "") {
$AdFile[$Count1] = $Ad;
$Count1++;
}
 
A

asm_toronto

Came up with my own by removing the spaces (nasty)

my @aro = map { join '|', @$_ }
sort {



$b->[3] =~ s/ //g;
$a->[3] =~ s/ //g;



lc($a->[3]) cmp lc($b->[3]) }
map { [ split /\|/ ] } <DATA>;
$Count1 = 0;
@AdFile = "";

foreach $Ad (@aro) {
@Field = split (/\|/, $Ad);
if ($Field[0] ne "") {
$AdFile[$Count1] = $Ad;
$Count1++;
}
}
 
T

takarov2003

asm_toronto said:
I have a datafile (over which I have no control) with a lot of leading
blank spaces
in certian records, which affects my alphabetic sorts.

Can someone help me make this routine ignore leading spaces?

open (DATA, "$DataDir/offline.txt");

my @aro = map { join '|', @$_ }
sort { lc($a->[1]) cmp lc($b->[1]) }

map { [ split /\|/ ] } <DATA>;

foreach $Ad (@aro) {
@Field = split (/\|/, $Ad);
if ($Field[0] ne "") {
$AdFile[$Count1] = $Ad;
$Count1++;
}


This is in the perlFAQ, available with the distribution, or at
http://faq.perl.org/perlfaq4.html#How_do_I_strip_blank

s/^\s+//;
 
B

Brad Murray

at> Came up with my own by removing the spaces (nasty)

Why not just delete leading spaces?

s/^\s+//;
 
X

Xicheng

asm_toronto said:
I have a datafile (over which I have no control) with a lot of leading
blank spaces
in certian records, which affects my alphabetic sorts.

Can someone help me make this routine ignore leading spaces?

open (DATA, "$DataDir/offline.txt");
as => my @aro = map { join '|', @$_ }
as => sort { lc($a->[1]) cmp lc($b->[1]) }
change this:
as => map { [ split /\|/ ] } <DATA>;
to
map { [ split /\|\s*/ ] }
map { s/\G\s//g; $_ } <DATA>;
or
map { [ split /\|/ ] }
map { s/(^|(?<=\|))\s*//g; $_ } <DATA>'

(untested)

Xicheng
foreach $Ad (@aro) {
@Field = split (/\|/, $Ad);
if ($Field[0] ne "") {
$AdFile[$Count1] = $Ad;
$Count1++;
}
 
X

Xicheng Jia

Xicheng said:
asm_toronto said:
I have a datafile (over which I have no control) with a lot of leading
blank spaces
in certian records, which affects my alphabetic sorts.

Can someone help me make this routine ignore leading spaces?

open (DATA, "$DataDir/offline.txt");
as => my @aro = map { join '|', @$_ }
as => sort { lc($a->[1]) cmp lc($b->[1]) }
change this:
as => map { [ split /\|/ ] } <DATA>;

xc => map { [ split /\|\s*/ ] }
xc => map { s/\G\s//g; $_ } <DATA>;

Sorry, these two 'map's can be written into one:

map { s/\G\s//g; [ split /\|\s*/ ] } <DATA>;
or
map { s/(^|(?<=\|))\s*//g; [ split /\|/ ] } <DATA>'

(untested)
Xicheng
or
map { [ split /\|/ ] }
map { s/(^|(?<=\|))\s*//g; $_ } <DATA>'

(untested)

Xicheng
foreach $Ad (@aro) {
@Field = split (/\|/, $Ad);
if ($Field[0] ne "") {
$AdFile[$Count1] = $Ad;
$Count1++;
}
 
J

John W. Krahn

asm_toronto said:
I have a datafile (over which I have no control) with a lot of leading
blank spaces
in certian records, which affects my alphabetic sorts.

Can someone help me make this routine ignore leading spaces?

open (DATA, "$DataDir/offline.txt");

You should *ALWAYS* verify that the file opened correctly before trying to use
the filehandle:

open DATA, "$DataDir/offline.txt"
or die "Cannot open '$DataDir/offline.txt' $!";
my @aro = map { join '|', @$_ }
sort { lc($a->[1]) cmp lc($b->[1]) }

map { [ split /\|/ ] } <DATA>;

my @aro = map $_->[ 0 ],
sort { $a->[ 1 ] cmp $b->[ 1 ] }
map [ $_, lc +( split /\s*\|\s*/ )[ 1 ] ],
<DATA>;



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top