how to show the fragment of element from array ?

E

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
 
J

Josef Moellers

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
 
E

esska

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!
 
E

esska

Strictly speaking, this is not a 3-dimensional array but a 1-dimensional
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", ...
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.
 
M

Michele Dondi

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
 
J

Josef Moellers

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
 
D

Dave Weaver

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top