Modifying elements of an array

Discussion in 'Perl Misc' started by fenisol3, Apr 2, 2004.

  1. fenisol3

    fenisol3 Guest

    I assume the last element of my array has some junk characters attached to
    it. So, how do I zap away all the junk characters after the very last
    alphabet in my array? Thanks for helping out. Lets assume $headings[5] is
    the last element of my array, I was thinking something like this:
    "$headings1[5] =~ s/[a-zA-Z].*//;" but it doesn't work.
    fenisol3, Apr 2, 2004
    1. Advertisements

  2. This is yet another ambigous post from you.

    - What do you mean by "junk characters"?
    - What might the element contain before the change?

    Please study the posting guidelines for this group:

    Also please understand that it's good manners to follow-up your posts
    when people are trying to help, or else they won't be inclined to do
    so again.

    *Guessing* what it is you want, this may be one way:

    $array[-1] =~ s/[^A-Za-z]*$//;
    Gunnar Hjalmarsson, Apr 2, 2004
    1. Advertisements

  3. fenisol3

    Paul Lalli Guest

    What, exactly, makes you assume this? In Perl, an array element has only
    what you put into it. There are no random characters attached, like you
    might expect from dealing with character arrays in C/C++.

    What exactly are you trying to do? If you're trying to remove all
    elements after the 6th from an array, do this:

    @array = @array[0..5];
    splice (@array, 6);

    If on the other hand, your sixth array element contains letters followed
    by 'junk' you want to remove, you want to do this:

    $array[5] =~ s/^([a-zA-Z]*).*/$1/;

    Perhaps you should try to tell us again what you're trying to do, if I
    didn't understand correctly...

    Paul Lalli
    Paul Lalli, Apr 2, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.