Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to show the fragment of element from array ?

Reply
Thread Tools

how to show the fragment of element from array ?

 
 
esska@wp.pl
Guest
Posts: n/a
 
      09-25-2007
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

 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      09-25-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
esska@wp.pl
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 10:29 am, Josef Moellers <josef.moell...@fujitsu-
siemens.com> wrote:
> (E-Mail Removed) 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!

 
Reply With Quote
 
esska@wp.pl
Guest
Posts: n/a
 
      09-25-2007
On Sep 25, 10:44 am, (E-Mail Removed) wrote:
> On Sep 25, 10:29 am, Josef Moellers <josef.moell...@fujitsu-
>
>
>
> siemens.com> wrote:
> > (E-Mail Removed) 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.

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      09-25-2007
On Tue, 25 Sep 2007 01:44:02 -0700, (E-Mail Removed) 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,
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      09-25-2007
(E-Mail Removed) wrote:
> On Sep 25, 10:29 am, Josef Moellers <josef.moell...@fujitsu-
> siemens.com> wrote:
>
>>(E-Mail Removed) 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

 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      09-26-2007
On Tue, 25 Sep 2007 00:33:24 -0700, (E-Mail Removed) <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Adding an XML fragment as a child node in a pre-existing Element tree Rajarshi Python 1 02-11-2007 07:18 PM
Toggling Content in SPAN element, One is Text Node, Other Document Fragment User Access Approved Javascript 0 10-15-2004 02:39 PM
Loader Element in Tomcat Context Fragment causes "Servlet default is not available" Daniel Bove Java 1 04-26-2004 03:00 PM



Advertisments