how to show the fragment of element from array ?

how to show the fragment of element from array ?

am 25.09.2007 09:33:24 von esska

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

Re: how to show the fragment of element from array ?

am 25.09.2007 10:29:43 von Josef Moellers

esska@wp.pl 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=20
way you can achieve that: Imagine you effectively pushed=20
"1,2,3,4,5,6,7": Now was that $a=3D1, $b=3D2, $c=3D"3,4,5,6,7" or was tha=
t=20
$a=3D"1,2", $b=3D3, $c=3D"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=3D", (split(/,/, $array[0],2))[0], "\n";

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

Josef
--=20
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

Re: how to show the fragment of element from array ?

am 25.09.2007 10:44:02 von esska

On Sep 25, 10:29 am, Josef Moellers siemens.com> wrote:
> es...@wp.pl 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=3D1, $b=3D2, $c=3D"3,4,5,6,7" or was that
> $a=3D"1,2", $b=3D3, $c=3D"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=3D", (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. Pratc=
hett)
> 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] =3D~ /^(\S+),(\S+)/){
print "$1\n";
print "$2\n";
}

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

Thank you for advice!

Re: how to show the fragment of element from array ?

am 25.09.2007 10:46:04 von esska

On Sep 25, 10:44 am, es...@wp.pl wrote:
> On Sep 25, 10:29 am, Josef Moellers >
>
>
> siemens.com> wrote:
> > es...@wp.pl 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=3D1, $b=3D2, $c=3D"3,4,5,6,7" or was t=
hat
> > $a=3D"1,2", $b=3D3, $c=3D"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=3D", (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. Pra=
tchett)
> > 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] =3D~ /^(\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.

Re: how to show the fragment of element from array ?

am 25.09.2007 12:41:18 von Michele Dondi

On Tue, 25 Sep 2007 01:44:02 -0700, esska@wp.pl 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^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: how to show the fragment of element from array ?

am 25.09.2007 14:32:34 von Josef Moellers

esska@wp.pl wrote:
> On Sep 25, 10:29 am, Josef Moellers > siemens.com> wrote:
>=20
>>es...@wp.pl 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-dimensiona=
l
>>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=3D1, $b=3D2, $c=3D"3,4,5,6,7" or was t=
hat
>>$a=3D"1,2", $b=3D3, $c=3D"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=3D", (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. Pra=
tchett)
>>Company Details:http://www.fujitsu-siemens.com/imprint.html
>=20
>=20
> 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";
> }
>=20
> but I'm not sure if it's the most optimize version :)

It's neither optimized nor correct:

use warnings;
use strict;
my @array =3D ("a,b,c");
my $i =3D 0;
if ($array[$i] =3D~ /^(\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=20
the first \S+ will match everything until the last comma, leaving the=20
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] =3D~ /^(\S+?),(\S+)/){
2) make the first regex match anything but a comma:
if ($array[$i] =3D~ /^([^,]+),(\S+)/){

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

Josef

--=20
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

Re: how to show the fragment of element from array ?

am 26.09.2007 10:03:32 von Dave Weaver

On Tue, 25 Sep 2007 00:33:24 -0700, esska@wp.pl 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