Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

simple perl question.

Options
  • 04-12-2008 10:02pm
    #1
    Registered Users Posts: 26,579 ✭✭✭✭


    i have an array populated with files of the current directory now the directory contains the following files (numbers are random).

    00
    200
    77
    100
    12
    23
    99
    90
    myperlfile.pl
    mytext.txt

    i want to get the array populated only by the files denoted by the digits.

    this is what i have so far.

    [php]
    my @filelist = <*>;

    my @temp;

    foreach my $file (@filelist)
    {
    if($file =~ m/^[0-9]*$/)
    {
    push(@temp, $file);
    }
    }

    @filelist = @temp;
    @filelist = sort(@filelist);

    foreach my $filelist (@filelist)
    {
    print $filelist;
    }
    [/php]

    this seems to work but the sort doesn't work like i want it too.

    on the above directory listing the above will print

    00 100 12 200 22 23 77 90 99

    where as i'd like it printed like this:

    00 12 22 23 77 90 99 100 200

    what i ultimately want to end up with is two variables with the lowest and highest value i.e. in the above example 00 200

    is there a simpler way of doing this? as i think i've went a bit nuts with it?


Comments

  • Registered Users Posts: 23,212 ✭✭✭✭Tom Dunne


    Ah yes, the perennial ASCII value vs. numeric value problem.

    What it's doing is correct - it's ignoring the numeric value and sorting them on ASCII values. Convert the number portions to numbers and it will sort correctly. Can't remember how to do it, it's been a while since I looked at perl.

    I actually did this in SQL only there a few weeks back. :)


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Tom Dunne wrote: »
    Ah yes, the perennial ASCII value vs. numeric value problem.

    What it's doing is correct - it's ignoring the numeric value and sorting them on ASCII values. Convert the number portions to numbers and it will sort correctly. Can't remember how to do it, it's been a while since I looked at perl.

    I actually did this in SQL only there a few weeks back. :)
    To follow on from this:

    [php]

    my @filelist = <*>;

    my @temp;

    foreach my $file (@filelist)
    {
    if($file =~ m/^[0-9]*$/)
    {
    push(@temp, $file);
    }
    }

    @filelist = @temp;
    @filelist = sort (sortByNumber @filelist); # Use custom comparer function

    foreach my $filelist (@filelist)
    {
    print $filelist;
    }

    sub sortByNumber
    {
    $a <=> $b; # <--- numeric compare
    }

    [/php]

    Havn't tried it but I presume it works, though it quite late...


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    superb guys, after a bit of googling after Tom's post i came across your solution Webmonkey.


    Cheers.


  • Registered Users Posts: 10 knackee


    print join "\n", sort { $a <=> $b } grep { /^\d+$/ } <*>;
    
    From right to left:
    1) <*> get all files
    2) grep (find) all numeric filenames
    3) sort numerically
    4) print results separated by newlines


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    ok, hit another snag on this.

    i've got it down to the numeric files will only be in the range of 1..20 or 1..X.

    now what i have at the moment is i can run my perl file like so.

    [php]perl myperlfile.pl -d 13[/php]

    and that will delete file 13 from the directory. what i would like is that when this action is done i want file 14,15,16,17,18,19,20 to be renamed to 13,14,15,16,17,18,19.

    i'm not at the machine now that contains the script but looking for pointers on how to go about doing this.

    what i was thinking was reading the directory contents like before into an array, deleting the file, then reading the contents of the directory again and doing some compare between the two?


  • Advertisement
Advertisement