in reply to Split an array -> store contents into a scalar up till a certain character

The easiest way I can think of would be if the address were in a string, and you split it into the array. Doing that could be as simple as @octets = split(/\D+/, $ipaddress, 4);, with the ', 4' possibly unnecessary, but forcing split() to return only 4 elements.

Hope that helps.

  • Comment on Re: Split an array -> store contents into a scalar up till a certain character
  • Download Code

Replies are listed 'Best First'.
Re: Re: Split an array -> store contents into a scalar up till a certain character
by FireBird34 (Pilgrim) on Oct 19, 2002 at 23:18 UTC
    Ok, thanks all. I have it now.

    my ( $scalar_1, $scalar_2, $scalar_3, $scalar_4 ) = split /\./, '25.255.2.0';

    That did what I needed.
      I'd advise against using four variables, and instead, would advocate using a single array. For example:
      my $addr = "25.255.2.0"; my @scalar = split(/\./, $addr);
      Then later, you can use it just as you would a regular scalar:
      print "The address is $scalar[0].$scalar[1].$scalar[2].$scalar[3]\n";
      There are many advantages to using an array instead of multiple scalars, a simple one being that this could also be expressed more simply as:
      print "The address is ",join('.',@scalar),"\n";
      There are few differences between $scalar[1] and $scalar_2 otherwise.
        Hmm... makes more sense. I'll try that method out. To add onto the rest of what I'm doing, this might make things alot easier.