Web Analytics Made Easy -
StatCounter problem with Numeric sort routine - CodingForum

Announcement

Collapse
No announcement yet.

problem with Numeric sort routine

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem with Numeric sort routine

    I am reading a DIR and grabing the image file names
    and then generating an HTML Doc with a javascript SlideShow that looks like this.

    SLIDES = new Array();
    SLIDES[0] = ["../../RE/images/single/06-333222/tn/06-333222-10.jpg"];
    SLIDES[1] = ["../../RE/images/single/06-333222/tn/06-333222-11.jpg"];
    SLIDES[2] = ["../../RE/images/single/06-333222/tn/06-333222-12.jpg"];
    SLIDES[3] = ["../../RE/images/single/06-333222/tn/06-333222-2.jpg"];
    SLIDES[4] = ["../../RE/images/single/06-333222/tn/06-333222-3.jpg"];
    SLIDES[5] = ["../../RE/images/single/06-333222/tn/06-333222-4.jpg"];
    SLIDES[6] = ["../../RE/images/single/06-333222/tn/06-333222-5.jpg"];
    SLIDES[7] = ["../../RE/images/single/06-333222/tn/06-333222-6.jpg"];
    SLIDES[8] = ["../../RE/images/single/06-333222/tn/06-333222-7.jpg"];
    SLIDES[9] = ["../../RE/images/single/06-333222/tn/06-333222-8.jpg"];
    SLIDES[10] = ["../../RE/images/single/06-333222/tn/06-333222-9.jpg"];


    06-333222-1.jpg does exist, but, i am Not displaying it in the SS.

    My problem is this.
    I dont know in advance how many images i will be displaying.
    I want the code to look like this:

    SLIDES = new Array();
    SLIDES[0] = ["../../RE/images/single/06-333222/tn/06-333222-2.jpg"];
    SLIDES[1] = ["../../RE/images/single/06-333222/tn/06-333222-3.jpg"];
    SLIDES[2] = ["../../RE/images/single/06-333222/tn/06-333222-4.jpg"];
    SLIDES[3] = ["../../RE/images/single/06-333222/tn/06-333222-5.jpg"];
    SLIDES[4] = ["../../RE/images/single/06-333222/tn/06-333222-6.jpg"];
    SLIDES[5] = ["../../RE/images/single/06-333222/tn/06-333222-7.jpg"];
    SLIDES[6] = ["../../RE/images/single/06-333222/tn/06-333222-8.jpg"];
    SLIDES[7] = ["../../RE/images/single/06-333222/tn/06-333222-9.jpg"];
    SLIDES[8] = ["../../RE/images/single/06-333222/tn/06-333222-10.jpg"];
    SLIDES[9] = ["../../RE/images/single/06-333222/tn/06-333222-11.jpg"];
    SLIDES[10] = ["../../RE/images/single/06-333222/tn/06-333222-12.jpg"];

    I tried a number sort.

    Code:
    @imagefiles2 = sort { $a <=> $b } @imagefiles;
    @imagefiles = @imagefiles2;
    Are the "-" messing me up?

    I tested my sort with this, and it worked fine:
    Code:
    #!/perl/bin/perl
    print "Content-type: text/html\n\n";
    
    @imagefiles = (1,20,2,10,3);
    @imagefiles2 = sort { $a <=> $b } @imagefiles;
    @imagefiles = @imagefiles2;
    
          foreach my $i (0..$#imagefiles) {
             print "image $i is $imagefiles[$i]<br>";
          }
    I want the "-" to be part of the numbering scheme.
    How can i get around this and still do a true number sort?

    As always, Thanks for looking.
    Have a great day.

  • #2
    I'm a php programmer so i'm not sure what the exact syntax is...but if you're files all end with -xx.jpg then why not...

    PHP Code:
    foreach....... {
         
    preg_match('/-([0-9+])\.jpg/',$filename,$match);
         
    $imagefiles[$match[0]] = $filename;

    Should work as long as you're not using a LCV to iterate thru imagefiles. Again, this is based on PHP logic, but i'm sure the functions are available to you.
    Last edited by syosoft; Sep 30, 2006, 05:01 PM.
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

    Comment


    • #3
      assuming you have all the image filenames in an array already (@imagefiles):

      Code:
      @sorted_image_files = map {$_->[0]}
                            sort {$a->[1] <=> $b->[1]}
                            map {chomp; /-(\d+).jpg$/; [$_,$1]}  @imagefiles;
      
      print "SLIDES = new Array();\n";
      foreach my $i (0.. $#sorted_image_files) {
         print qq~SLIDES[$i] = ["../../RE/images/single/06-333222/tn/$sorted_image_files[$i]"]\n~;
      }

      Comment


      • #4
        WTG Kevin
        Fantastic, Thank You.


        Thanks syosoft.

        Comment


        • #5
          yer welcome

          Comment


          • #6
            I tried
            Active PHP/MySQL application developer available for immediate work.
            syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

            Comment


            • #7
              Originally posted by syosoft View Post
              I tried
              It will help to learn perl to answer perl questions.

              Comment

              Working...
              X