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

Given the address of 25.255.2.0 in @array, I want to be able to store 25 into $scalar_1, 255 into $scalar_2, 2 into $scalar_3 and 0 into $scalar_4. It *should* be fairly easy to do, but not being a Perl guru I don't know how. I could do this using if statements, but that is way to tedious and takes up to much room/space. So basically, everything up to the first "." I wanted stored in my first scalar (then chopped off), then everything up to the next "." into a second scalar, etc, leaving the leftovers (0) into the fourth scalar. If anyone knows how, please let me know. Thanks.
  • Comment on Split an array -> store contents into a scalar up till a certain character

Replies are listed 'Best First'.
Re: Split an array -> store contents into a scalar up till a certain character
by rob_au (Abbot) on Oct 19, 2002 at 23:08 UTC
    I'm not exactly sure as to how you have the value of 25.255.2.0 stored, but if this is a scalar string, you can split it into array of the constituent components through use of the split command. These array elements can then be assigned to the individual scalars thus:

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

    If however the individual components of the string 25.255.2.0 are already stored in separate array elements, then the following should suffice:

    my ( $scalar_1, $scalar_2, $scalar_3, $scalar_4 ) = @array;

     

    perl -e 'print+unpack("N",pack("B32","00000000000000000000000111010100")),"\n"'

Re: Split an array -> store contents into a scalar up till a certain character
by nothingmuch (Priest) on Oct 19, 2002 at 23:07 UTC
    The split function does exactly that.
    my ($scalar_1,$scalar_2,$scalar_3,$scalar_4) = split(/\./,$array[0]); +# is it [0]?


    -nuffin
    zz zZ Z Z #!perl
Re: Split an array -> store contents into a scalar up till a certain character
by atcroft (Abbot) on Oct 19, 2002 at 23:08 UTC

    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.

      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.
Re: Split an array -> store contents into a scalar up till a certain character
by dug (Chaplain) on Oct 19, 2002 at 23:33 UTC
    This should do it.
    my($quad_1, $quad_2, $quad_3, $quad_4) = split /\./, $array[0];

    Of course, I'm assuming that what you mean by having the address 25.255.2.0 in @array, is that the first element of @array is the string '25.255.2.0'. That would be the case if you did something like @array = '25.255.2.0' in order to get the address into your array.

    If the address is in $string, simply replace $array[0] (the first element of @array) with $string and you should be good to go.

    If what you mean is that the address was split into four elements and assigned to @array, something like @array = split /\./, '25.255.2.0';, then you can either just use the array indices directly, or assign them into temporary variables, like so:
    $array[0] == '25' and print "The first quadrant is 25\n"; $quad_1 = $array[0]; $quad_2 = $array[1]; $quad_3 = $array[2]; $quad_4 = $array[3]; $quad_1 == '25' and print "The first quadrant is still 25\n";

      dug