how to show the fragment of element from array ?

Discussion in 'Perl Misc' started by esska@wp.pl, Sep 25, 2007.

  1. Guest

    Hi all,
    I have problem with 3-dimensional array:
    push(@array,"$a,$b,$c");
    I don't know how to show only $a from first element of this array.
    Anyone could who knows answer, I will be grateful.

    Regards,
    esska
    , Sep 25, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi all,
    > I have problem with 3-dimensional array:
    > push(@array,"$a,$b,$c");


    Strictly speaking, this is not a 3-dimensional array but a 1-dimensional
    array.

    > I don't know how to show only $a from first element of this array.


    It depends upon the contents of $a: if $a contains commas, there is no
    way you can achieve that: Imagine you effectively pushed
    "1,2,3,4,5,6,7": Now was that $a=1, $b=2, $c="3,4,5,6,7" or was that
    $a="1,2", $b=3, $c="4,5,6,7", ...

    > Anyone could who knows answer, I will be grateful.


    If you can guarantee that $a does not contain commas:
    print "Result=", (split(/,/, $array[0],2))[0], "\n";

    If you cannot guarantee that, you might want to have a look at csv
    (Text::CSV_XS).

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Sep 25, 2007
    #2
    1. Advertising

  3. Guest

    On Sep 25, 10:29 am, Josef Moellers <josef.moell...@fujitsu-
    siemens.com> wrote:
    > wrote:
    > > Hi all,
    > > I have problem with 3-dimensional array:
    > > push(@array,"$a,$b,$c");

    >
    > Strictly speaking, this is not a 3-dimensional array but a 1-dimensional
    > array.
    >
    > > I don't know how to show only $a from first element of this array.

    >
    > It depends upon the contents of $a: if $a contains commas, there is no
    > way you can achieve that: Imagine you effectively pushed
    > "1,2,3,4,5,6,7": Now was that $a=1, $b=2, $c="3,4,5,6,7" or was that
    > $a="1,2", $b=3, $c="4,5,6,7", ...
    >
    > > Anyone could who knows answer, I will be grateful.

    >
    > If you can guarantee that $a does not contain commas:
    > print "Result=", (split(/,/, $array[0],2))[0], "\n";
    >
    > If you cannot guarantee that, you might want to have a look at csv
    > (Text::CSV_XS).
    >
    > Josef
    > --
    > These are my personal views and not those of Fujitsu Siemens Computers!
    > Josef Möllers (Pinguinpfleger bei FSC)
    > If failure had no penalty success would not be a prize (T. Pratchett)
    > Company Details:http://www.fujitsu-siemens.com/imprint.html


    hi,
    element doesn't contain any commas :)
    I have separate elements by using pattern e.g.
    if ($array[$i] =~ /^(\S+),(\S+)/){
    print "$1\n";
    print "$2\n";
    }

    but I'm not sure if it's the most optimize version :)

    Thank you for advice!
    , Sep 25, 2007
    #3
  4. Guest

    On Sep 25, 10:44 am, wrote:
    > On Sep 25, 10:29 am, Josef Moellers <josef.moell...@fujitsu-
    >
    >
    >
    > siemens.com> wrote:
    > > wrote:
    > > > Hi all,
    > > > I have problem with 3-dimensional array:
    > > > push(@array,"$a,$b,$c");

    >
    > > Strictly speaking, this is not a 3-dimensional array but a 1-dimensional
    > > array.

    >
    > > > I don't know how to show only $a from first element of this array.

    >
    > > It depends upon the contents of $a: if $a contains commas, there is no
    > > way you can achieve that: Imagine you effectively pushed
    > > "1,2,3,4,5,6,7": Now was that $a=1, $b=2, $c="3,4,5,6,7" or was that
    > > $a="1,2", $b=3, $c="4,5,6,7", ...

    >
    > > > Anyone could who knows answer, I will be grateful.

    >
    > > If you can guarantee that $a does not contain commas:
    > > print "Result=", (split(/,/, $array[0],2))[0], "\n";

    >
    > > If you cannot guarantee that, you might want to have a look at csv
    > > (Text::CSV_XS).

    >
    > > Josef
    > > --
    > > These are my personal views and not those of Fujitsu Siemens Computers!
    > > Josef Möllers (Pinguinpfleger bei FSC)
    > > If failure had no penalty success would not be a prize (T. Pratchett)
    > > Company Details:http://www.fujitsu-siemens.com/imprint.html

    >
    > hi,
    > element doesn't contain any commas :)
    > I have separate elements by using pattern e.g.
    > if ($array[$i] =~ /^(\S+),(\S+)/){
    > print "$1\n";
    > print "$2\n";
    >
    > }
    >
    > but I'm not sure if it's the most optimize version :)
    >
    > Thank you for advice!


    I forgot to say that value of elements is changing so sometimes $b
    parameter can have value of 3 and sometimes 5 etc.
    , Sep 25, 2007
    #4
  5. On Tue, 25 Sep 2007 01:44:02 -0700, wrote:

    >hi,
    >element doesn't contain any commas :)
    >I have separate elements by using pattern e.g.
    > if ($array[$i] =3D~ /^(\S+),(\S+)/){
    > print "$1\n";
    > print "$2\n";
    >}


    You probably want split() instead. See

    perldoc -f split

    and ask again if you can't understand something.

    But why don't you use a "real" multidimensional array instead?

    >but I'm not sure if it's the most optimize version :)


    Optimized... for what?


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Sep 25, 2007
    #5
  6. wrote:
    > On Sep 25, 10:29 am, Josef Moellers <josef.moell...@fujitsu-
    > siemens.com> wrote:
    >
    >> wrote:
    >>
    >>>Hi all,
    >>>I have problem with 3-dimensional array:
    >>>push(@array,"$a,$b,$c");

    >>
    >>Strictly speaking, this is not a 3-dimensional array but a 1-dimensional
    >>array.
    >>
    >>
    >>>I don't know how to show only $a from first element of this array.

    >>
    >>It depends upon the contents of $a: if $a contains commas, there is no
    >>way you can achieve that: Imagine you effectively pushed
    >>"1,2,3,4,5,6,7": Now was that $a=1, $b=2, $c="3,4,5,6,7" or was that
    >>$a="1,2", $b=3, $c="4,5,6,7", ...
    >>
    >>
    >>>Anyone could who knows answer, I will be grateful.

    >>
    >>If you can guarantee that $a does not contain commas:
    >>print "Result=", (split(/,/, $array[0],2))[0], "\n";
    >>
    >>If you cannot guarantee that, you might want to have a look at csv
    >>(Text::CSV_XS).
    >>
    >>Josef
    >>--
    >>These are my personal views and not those of Fujitsu Siemens Computers!
    >>Josef Möllers (Pinguinpfleger bei FSC)
    >> If failure had no penalty success would not be a prize (T. Pratchett)
    >>Company Details:http://www.fujitsu-siemens.com/imprint.html

    >
    >
    > hi,
    > element doesn't contain any commas :)
    > I have separate elements by using pattern e.g.
    > if ($array[$i] =~ /^(\S+),(\S+)/){
    > print "$1\n";
    > print "$2\n";
    > }
    >
    > but I'm not sure if it's the most optimize version :)


    It's neither optimized nor correct:

    use warnings;
    use strict;
    my @array = ("a,b,c");
    my $i = 0;
    if ($array[$i] =~ /^(\S+),(\S+)/){
    print "$1\n";
    print "$2\n";
    }
    a,b
    c

    Regex-en are "greedy", i.e. they try to match as much as possible, so
    the first \S+ will match everything until the last comma, leaving the
    rest for the second \S+.

    If you want to stay with a regex, there are (at least) two possibilities
    (TMTTWTDI ;-):
    1) make the regex non-greedy:
    if ($array[$i] =~ /^(\S+?),(\S+)/){
    2) make the first regex match anything but a comma:
    if ($array[$i] =~ /^([^,]+),(\S+)/){

    But I guess using split is much better and matches your problem better
    ("give me the first element of a comma-separated list"), but the
    Text::CSV_XS would be even better.

    Josef

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Sep 25, 2007
    #6
  7. Dave Weaver Guest

    On Tue, 25 Sep 2007 00:33:24 -0700, <> wrote:
    > Hi all,
    > I have problem with 3-dimensional array:
    > push(@array,"$a,$b,$c");
    > I don't know how to show only $a from first element of this array.
    > Anyone could who knows answer, I will be grateful.


    You are storing $a, $b and $c in a string. This is not the best
    approach if you're just trying to store those values and retrieve them
    later.

    Better would be to store them in an (anonymous) array:

    push @array, [ $a, $b, $c ];

    print $array[0][0], $array[0][2]; # gives $a & $c from the first
    # element of the arrray

    or use an (anonymous) hash:

    push @array, { a => $a, b => $b, c => $c };

    print $array[0]{a}, $array[0]{c} # gives $a & $c from the first
    # element of the array
    Dave Weaver, Sep 26, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Bove
    Replies:
    1
    Views:
    2,399
    Daniel Bove
    Apr 26, 2004
  2. Rajarshi
    Replies:
    1
    Views:
    526
    Gabriel Genellina
    Feb 11, 2007
  3. HANM
    Replies:
    2
    Views:
    693
    Joseph Kesselman
    Jan 29, 2008
  4. Szabolcs Borsanyi
    Replies:
    6
    Views:
    651
    Ben Bacarisse
    May 23, 2008
  5. User Access Approved
    Replies:
    0
    Views:
    132
    User Access Approved
    Oct 15, 2004
Loading...

Share This Page