Need help to ignore leading spaces

Discussion in 'Perl Misc' started by asm_toronto, Apr 4, 2006.

  1. asm_toronto

    asm_toronto Guest

    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++;
    }
     
    asm_toronto, Apr 4, 2006
    #1
    1. Advertising

  2. asm_toronto

    asm_toronto Guest

    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++;
    }
    }
     
    asm_toronto, Apr 4, 2006
    #2
    1. Advertising

  3. asm_toronto

    Guest

    asm_toronto wrote:
    > 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+//;
     
    , Apr 4, 2006
    #3
  4. asm_toronto

    Brad Murray Guest

    asm_toronto <> wrote:
    at> Came up with my own by removing the spaces (nasty)

    Why not just delete leading spaces?

    s/^\s+//;

    --
    Brad Murray * <stimps> I once puked Tab
    VSCA Founder * <stimps> I was a kid and went in to get a bottle of root
    beer, and grabbed the WRONG BROWN SODA
     
    Brad Murray, Apr 4, 2006
    #4
  5. asm_toronto

    Xicheng Guest

    asm_toronto wrote:
    > 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++;
    > }
     
    Xicheng, Apr 4, 2006
    #5
  6. asm_toronto

    Xicheng Jia Guest

    Xicheng wrote:
    > asm_toronto wrote:
    > > 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++;
    > > }
     
    Xicheng Jia, Apr 4, 2006
    #6
  7. asm_toronto wrote:
    > 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
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Apr 5, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Nagle
    Replies:
    4
    Views:
    597
    James Antill
    Aug 7, 2007
  2. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    568
    Delaney, Timothy (Tim)
    Aug 7, 2007
  3. John B. Matthews
    Replies:
    4
    Views:
    669
    John B. Matthews
    Sep 12, 2008
  4. Roedy Green
    Replies:
    3
    Views:
    626
  5. johkar
    Replies:
    2
    Views:
    2,902
    Mayeul
    Dec 10, 2009
Loading...

Share This Page