How to create an array from a string?

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default How to create an array from a string?

    Hello.

    I would like to create an array from a string. I tried with:
    ---------------------------
    $string = "1 2 3";
    @array1 = [1,2,3];
    @array2 = split(/\s/, $string);
    print $array1[0] . " - " . $array1[1] . " - " . $array1[2] . "\n";
    print $array2[0] . " - " . $array2[1] . " - " . $array2[2] . "\n";
    ---------------------------

    But I get:
    ----------------------------
    ARRAY(0x1674e9c) - -
    1 - 2 - 3
    -----------------------------

    I would like to generate an array of the same type of 'array1' from
    'string'. Is it possible?

    Thank you very much.
    Francesco Moi Guest

  2. Similar Questions and Discussions

    1. Array String Property
      Hi. I'm newer in asp.net. I doing a derived control of TextBox. well y using a property of array string. But when i use this property in design...
    2. Is there a way to replace characters in a string/array
      I am trying to find a function similar to php's str_replace is there something that will allow me to replace a certain character in a variable? ...
    3. Cannot create an object of type 'System.String[]' from its representation 'String[] Array'
      Hello, I am designing a .net custom control in VS.net 7.1 and my control exposes an array of strings which are supposed to be the items to show. To...
    4. string concatenation from array
      I'm having a bit of difficulty getting a string to attach to itself from an array. Here is the bit of code I'm working on. $wresult = ""; ...
    5. [PHP] string concatenation from array
      Analysis & Solutions <mailto:danielc@analysisandsolutions.com> on Tuesday, August 12, 2003 1:02 PM said: Don't forget the space: $wresult .=...
  3. #2

    Default Re: How to create an array from a string?

    Francesco Moi wrote:
    >
    > @array1 = [1,2,3];
    ------------^-----^

    Hmm.. Don't you mean:

    @array1 = (1,2,3);

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  4. #3

    Default Re: How to create an array from a string?

    Francesco Moi <francescomoi@europe.com> wrote:
    > I would like to create an array from a string.

    That is a bad thing to want.

    Luckily, that isn't really what you want.

    You want to _populate_ an array from a string, not create an array.

    Yes?


    my @array1 = split /\s+/, "1 2 3";


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  5. #4

    Default Re: How to create an array from a string?


    "Francesco Moi" <francescomoi@europe.com> wrote in message
    news:5b829932.0308151533.18c7de64@posting.google.c om...
    > Hello.
    >
    > I would like to create an array from a string. I tried with:
    There is more than one way to do it.....

    If you want to use some form of literal to enumerate the values assigned to
    elements of an array, you can do any of the following:

    The list method:

    my @array = ( 1, 2, 3 );


    The string method:

    my @array = split /\s+/, "1 2 3";

    The list constructor operator method:

    my @array = ( 1 .. 3 );

    The word method:

    my @array = qw(1 2 3);

    There are others that deal with looping and such. But these ought to be
    enough.



    David Oswald Guest

  6. #5

    Default Re: How to create an array from a string?

    Francesco Moi wrote:
    > I need to create an _anonymous_ array (sorry, I din't mention it)
    > to be included in @data, e.g.:
    >
    > @data = (['John','Peter','Sophie'],[1,2,3]);
    $string = "1 2 3";
    @data = (['John','Peter','Sophie'], [split /\s/, $string]);

    print "$data[0]->[0] - $data[0]->[1] - $data[0]->[2]\n";
    print "$data[1]->[0] - $data[1]->[1] - $data[1]->[2]\n";

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  7. #6

    Default Re: How to create an array from a string?

    Francesco Moi <francescomoi@europe.com> wrote:
    > So I would like to create an anonymous array from a string.

    [ split /\s+/, "1 2 3" ]


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139