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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

simple perl question.

  • 04-12-2008 9:02pm
    #1
    Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭


    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,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, 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,584 ✭✭✭✭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,584 ✭✭✭✭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