Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 4 of 4

This is a discussion on Anybody know their arrays? in the Shared & Semi-Dedicated forum
I have an array like below: @array = ("123", "Main Street", "Denver", "456", "North Street", "Chicago") How do I do a loop whereas I put ...

  1. #1
    Loyal Client
    Join Date
    Aug 2002
    Posts
    269

    Anybody know their arrays?

    I have an array like below:

    @array = ("123", "Main Street", "Denver", "456", "North Street", "Chicago")

    How do I do a loop whereas I put the components of this array into three variables such as:

    $street_number = @array[0];
    $street = @array[1];
    $city = @array[2];

    #Process this stuff......

    #Loop back and get the next 3.

    I know this is simple, but I'm getting confused as someone (on another board) directed me to using 'foreach' but that doesn't seem to be working, and we've been back and forth and I'm just not getting anywhere.

    Thanks
    Sam

  2. #2
    JPC Member
    Join Date
    May 2003
    Location
    Canada
    Posts
    46
    Could do something like

    Code:
    for (my $i = 0; $i < @array; $i += 3)
    {
        ($street_number, $street, $city) = (@array[$i, $i + 1, $i + 2]);
    
        # Process stuff here
    }
    The nice thing about Perl is there's a million ways to solve the same problem. My solution above uses a "for" loop that increments by 3, and uses an array slice to initialize all three address variables at the same time.

    Just a quick note: your syntax of

    Code:
        $street_number = @array[0];
    is wrong, and will likely cause you no end of trouble. You only use the "@" when declaring the array with "my", when refering to the number of elements in the array (like I did in my loop condition), or when you mean to refer to the array itself (not single elements). Other wise use just a "$" like other variables:

    Code:
        $street_number = $array[0];
    
        $element_count = @array
    Hope that helps.

    - Darryl
    Last edited by Darryl; 05-18-2003 at 12:31 AM.

  3. #3
    Loyal Client
    Join Date
    Aug 2002
    Posts
    269
    Thank you very much. I'm having a tough time with arrays. I learned Basic back in the mid 80's, and it has caused me nothing but trouble when trying to learn modern languages.

    On the other board, this guy said I needed to "dereference" the array. That's when the few details I learned about arrays really went out the window. Basically I think I need a better book than the one I have.

    Thanks again for your help.

    By the way, if you feel like responding, I'm confused about the "my" stuff. I thought that when you declare something as in:

    my $count=0;

    That the "my" was tied with the CGI module. I had actually used it in a script because some of the "example" script I pasted in which used "my" to declare all the variables, so I kept using it. But I had no idea why. In this script, I didn't use "my" on anything and all the variables seem to still work properly.

    Sam

  4. #4
    JPC Member
    Join Date
    May 2003
    Location
    Canada
    Posts
    46
    The "my" slipped in there by accident. It's not really needed, and no it's got nothing to do with CGI. It's a way of declaring the variable and giving it scope. In my example above it means the variable $i is only valid within the scope of the loop (as opposed to being a global variable). It's perfectly fine not to use "my".

    Having said that however, the first line I write in all my perl scripts is

    Code:
    use strict;
    which forces you to do things like declare variables using "my" etc. I find it helps me write better code because the Perl interpreter will complain about undeclared variables, or variables used in the wrong scope. Leads to easier debugging. Just my 2 cents worth.

    If you don't mind a little reading, get the so called Llama ("Learning Perl") and Camel ("Perl Programming") books, both published by O'Reilly [They're called that because of the animal pictures used on the covers] They are the definitive books on the Perl language.

    - Darryl

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •