mexnix has asked for the wisdom of the Perl Monks concerning the following question:

Hola monks! I'm newbie programmer, Perl being my first learned language, and I working on a project at work that I'm having a problem with. I'm doing sort of a publication type thing but on a way smaller scale. I'm using text files with newline delimination for just plain easiness. If there's a better way, trying to stay away db's, smack me.

Anyway, I collect the info from the text file into an array through a open() command. I use some of the array throughout embedded html code (i.e. name, date written, author, etc), but the last part is the actual article (The idea is that paragraphs are also deliminated by newlines). Because the # of para's will change with the writer, I need to be able to just iterate over the list, but I don't want to display the whole array ($array[4] and beyond). I've tried for(), while(), and foreach(), with no luck.

If you can help, that would be great! I may just be an idiot, but I'm ok with that.

-Ryan

The array set up like this:

@array = ("img", "title", "date", "author", "para(1)",..."para(n)")

Replies are listed 'Best First'.
Re: print part of an array
by chromatic (Archbishop) on May 09, 2001 at 21:27 UTC
    It's hard to give a good answer without seeing more of your code, but I think you are asking "How can I skip the first four elements of the array and process all of the rest?"

    If that's the case, there are several options. Being a new programmer, you're probably already familiar with shift. You could use that to get at the img, title, date, and author fields. You could use splice to get rid of the first four elements. I like list slices, though:

    for my $para ((@array[4 .. $#array])) { # process $para }
    The trick there is that you're providing a list of indices of the array. Nothing terribly magical, but greatly useful.
Re: print part of an array
by srawls (Friar) on May 09, 2001 at 21:36 UTC
    >>Because the # of para's will change with the writer, I need to be able to just iterate over the list, but I don't want to display the whole array ($array4 and beyond). I've tried for(), while(), and foreach(), with no luck. >>

    I don't fully understand what you want. But here are some things you can do with your array format

    Process whole array:  for(each)? (@array)

    Process parts of an array:

    for (@array[0 .. 3]) #access contents in $_ I don't know if this is what you wanted, this is all the elements except for the paragraphs

    for (@array[4 .. $#array]) #this is all the paragraphs

    In your array, to print all of the paragraphs (is this what you wanted?), just use this:

    print "$_\n" for (@array[4 .. $#array]);


    The 15 year old, freshman programmer,
    Stephen Rawls
Re: print part of an array
by suaveant (Parson) on May 09, 2001 at 21:31 UTC
    yes, much like what chromatic said, @array[4..$#array] will return a list of all the values from index 4 to the end.
                    - Ant
Re: print part of an array
by mexnix (Pilgrim) on May 09, 2001 at 22:07 UTC
    Sorry about being a little vauge. I wasn't trying to be. I used shift and it worked great. More steps, no biggie. I'm not that good yet. Thanks a bunch.

    -Ryan