Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

simple perl question.

  • 04-12-2008 09:02PM
    #1
    Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭


    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, Registered Users 2 Posts: 23,202 ✭✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 26,449 ✭✭✭✭Creamy Goodness


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


    Cheers.


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 26,449 ✭✭✭✭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