Loading ...
Sorry, an error occurred while loading the content.

Ordering Elements in an Array

Expand Messages
  • Gregory D. Fox
    Hi All, I need help. I would like to have the @items array in a particular order. Right now the array looks like this: @items =CJ1 CJ2 FU1 FU2 0 3 21 1 I would
    Message 1 of 3 , Jun 3, 2000
    • 0 Attachment

      Hi All,

       

      I need help.

       

      I would like to have the @items array in a particular order.

       

      Right now the array looks like this:

       

      @items =CJ1 CJ2 FU1 FU2 0 3 21 1

       

      I would like this:

       

      @items =CJ1 0 CJ2 3 FU1 21 FU2 1

       

      Below is the code. Any help is greatly appreciated.

                 

      ----- BEGIN CODE -----

       

      #!/bin/perl -w
      use diagnostics;

       

      use CGI qw (:standard);
      use CGI::Carp qw(fatalsToBrowser);
      use strict;

       

      BEGIN {
         print CGI::header();
         open (STDERR, '>error.log');
      }

       

      my(@items,@itemcodes,@units,$key);
      @itemcodes = qw/CJ1 CJ2 FU1 FU2/;
      @units = qw/0 3 21 1/;

       

       foreach $key (@itemcodes, @units) {
               push @items, $key;
            }
        
      print "<H2>Here are the results</H2>";
         
         print qq{
         <p>
            \@items =@items
         };
         exit;


      --
      Gregory D. Fox
      Greg@...
      <>< <>< <>< <>< <>< ><>

      ><> ><> ><> ><>
      Liberty Computer Solutions/Liberty Marketing
      Web Site Hosting and Design
      <>< <>< <>< <>< <>< ><>
      ><> ><> ><> ><>
      (401) 823-5252/Fax: (401) 828-7057
      <>< <>< <>< <>< <>< ><>
      ><> ><> ><> ><>
      Visit our website at: www.libertymarketing.com
      <>< <>< <>< <>< <>< ><>
      ><> ><> ><> ><>
      color=black>

    • Andy Dougherty
      ... I m just guessing at your intentions, but would a hash structure be better suited to your needs? It looks to me like the @itemcodes and @units arrays are
      Message 2 of 3 , Jun 5, 2000
      • 0 Attachment
        On Sat, 3 Jun 2000, Gregory D. Fox wrote:

        > I would like to have the @items array in a particular order.
        > Right now the array looks like this:
        > @items =CJ1 CJ2 FU1 FU2 0 3 21 1
        > I would like this:
        > @items =CJ1 0 CJ2 3 FU1 21 FU2 1
        >
        > my(@items,@itemcodes,@units,$key);
        > @itemcodes = qw/CJ1 CJ2 FU1 FU2/;
        > @units = qw/0 3 21 1/;

        I'm just guessing at your intentions, but would a hash structure be
        better suited to your needs? It looks to me like the @itemcodes and
        @units arrays are paired up. Perhaps a single hash %items would be
        more useful? Something like

        my %items;
        my $key;
        %items = (
        'CJ1' => 0,
        'CJ2' => 3,
        'FU1' => 21,
        'FU2' => 1,
        );

        print "<H2>Here are the results</H2>\n<p>\n";

        foreach $key (sort keys %items) {
        print "$key $items{$key}\n";


        Hope this helps,

        Andy Dougherty doughera@...
        Dept. of Physics
        Lafayette College, Easton PA 18042
      • Smith, Sheldon
        Your foreach loop is doing what you told it: first, get each element out of @itemcodes, then get each element out of @units. Assuming @itemcodes and @units are
        Message 3 of 3 , Jun 5, 2000
        • 0 Attachment
          Your foreach loop is doing what you told it: first, get each element out of
          @itemcodes, then get each element out of @units.
          Assuming @itemcodes and @units are always the same length, do this instead.

          foreach $key (@itemcodes) {
          push @items, $key;
          push @items, shift @units;
          }


          -----Original Message-----
          From: Gregory D. Fox [mailto:greg@...]
          Sent: Saturday, June 03, 2000 10:10 AM
          To: perl-beginner@egroups.com
          Subject: [PBML] Ordering Elements in an Array



          Hi All,



          I need help.



          I would like to have the @items array in a particular order.



          Right now the array looks like this:



          @items =CJ1 CJ2 FU1 FU2 0 3 21 1



          I would like this:



          @items =CJ1 0 CJ2 3 FU1 21 FU2 1



          Below is the code. Any help is greatly appreciated.



          ----- BEGIN CODE -----



          #!/bin/perl -w
          use diagnostics;



          use CGI qw (:standard);
          use CGI::Carp qw(fatalsToBrowser);
          use strict;



          BEGIN {
          print CGI::header();
          open (STDERR, '>error.log');
          }



          my(@items,@itemcodes,@units,$key);
          @itemcodes = qw/CJ1 CJ2 FU1 FU2/;
          @units = qw/0 3 21 1/;



          foreach $key (@itemcodes, @units) {
          push @items, $key;
          }

          print "<H2>Here are the results</H2>";

          print qq{
          <p>
          \@items =@items
          };
          exit;


          --
          Gregory D. Fox
          Greg@... <mailto:Greg@...>
        Your message has been successfully submitted and would be delivered to recipients shortly.