Joining/Merging AoA

Joining/Merging AoA

am 22.04.2008 08:28:58 von Vishal G

Hi Guys,

I have a little complicated problem...

I have two arrays

@a = ( ['id', 'name', 'age'],
['1', 'Fred', '24'],
['2', 'Frank', '42'],
);

@b = ( ['id', 'sex'],
['1', 'm' ],
['2', 'm'],
);

I want to join these two AoA, based on id, so the resulting array will
look like this

@c = ( ['id', 'name', 'age', 'sex'],
['1', 'Fred', '24', 'm' ],
['2', 'Frank', '42', 'm'],
);

Any Ideas?

Thanks in advance.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

problem using backslash on brackets in regular expressions

am 23.04.2008 01:05:39 von Daniel McClory

Hi,

I have files which contain sentences, where some lines have extra
information inside brackets and parentheses. I would like to delete
everything contained within brackets or parentheses, including the
brackets. I know that I am supposed to use the backslash to turn off
the metacharacter properties of brackets and parentheses in a regular
expression.

I am trying to use the s/// operator to remove it, by doing this:

while()
{
$_ =~ s/\[*\]//;
$_ =~ s/\(*\)//;
print $_;
}

so if the input is:
*MOT: I'm gonna first [//] first I wanna use em all up .

then the output I'd like to get is:
*MOT: I'm gonna first first I wanna use em all up .

but instead what I get is:
*MOT: I'm gonna first [// first I wanna use em all up .

It only deletes the last piece, the ] bracket. How can I erase the
whole thing?

Thanks.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

RE: problem using backslash on brackets in regular expressions

am 23.04.2008 01:17:20 von David.Wagner

> -----Original Message-----
> From: Daniel McClory [mailto:danmcclory@gmail.com]=20
> Sent: Tuesday, April 22, 2008 16:06
> To: beginners@perl.org
> Subject: problem using backslash on brackets in regular expressions
>=20
> Hi,
>=20
> I have files which contain sentences, where some lines have extra =20
> information inside brackets and parentheses. I would like to delete =20
> everything contained within brackets or parentheses, including the =20
> brackets. I know that I am supposed to use the backslash to=20
> turn off =20
> the metacharacter properties of brackets and parentheses in a=20
> regular =20
> expression.
>=20
> I am trying to use the s/// operator to remove it, by doing this:
>=20
> while()
> {
> $_ =3D~ s/\[*\]//;
What you are saying here is the first bracket can have zero or
more occurances followed by a ], which is what you are seeing in your
output(ie, the / before the ] is not a [ okay, then ] and replace the ]
with nothing.

s/\[[^\]]+]//;
> $_ =3D~ s/\(*\)//;
s/\([^\)]+)//;

No reason to do the $_ =3D~ as by default that is what is going to
be done anyway.

Wags ;)

> print $_;
> }
>=20
> so if the input is:
> *MOT: I'm gonna first [//] first I wanna use em all up .
>=20
> then the output I'd like to get is:
> *MOT: I'm gonna first first I wanna use em all up .
>=20
> but instead what I get is:
> *MOT: I'm gonna first [// first I wanna use em all up .
>=20
> It only deletes the last piece, the ] bracket. How can I erase the =20
> whole thing?
>=20
> Thanks.
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>=20
>=20
>=20

************************************************************ **********
This message contains information that is confidential and proprietary to F=
edEx Freight or its affiliates. It is intended only for the recipient name=
d and for the express purpose(s) described therein. Any other use is proh=
ibited.
************************************************************ **********


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: problem using backslash on brackets in regular expressions

am 23.04.2008 01:22:06 von rvtol+news

Daniel McClory schreef:

> while()
> {
> $_ =~ s/\[*\]//;
> $_ =~ s/\(*\)//;
> print $_;
> }

while ( ) {
s/\[.*?\]//;
s/\(.*?\)//;
print;
}

--
Affijn, Ruud

"Gewoon is een tijger."

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

RE: problem using backslash on brackets in regular expressions

am 23.04.2008 01:58:50 von adarsh.s85

------=_NextPart_000_0001_01C8A502.EC934050
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hello,
(snip)
I am trying to use the s/// operator to remove it, by doing this:

while()
{
$_ =~ s/\[*\]//;
$_ =~ s/\(*\)//;
print $_;
}
(snip)

The method used is incorrect.
$_ =~ s/\[*\]//; ---> This says that the search is for opening
parenthesis (zero or more occurrences of it, since a '*' follows '[')
followed by a close parenthesis.

(snip)
so if the input is:
*MOT: I'm gonna first [//] first I wanna use em all up .
(snip)

In this input, considering ur search pattern, close parenthesis ']' is
found. Before that, the character is '/which is not '[' (zero occurrence
of it). Thus, it's a valid search. So only ']' is removed.

The correct search pattern ought to be:
$_ =~ s/\[.*\]//;
---> This shall search for "an opening parenthesis followed by zero or
more characters (.*) followed by a close parenthesis". So if the input
is

*MOT: I'm gonna first [//] first I wanna use em all up .
then output will be:
*MOT: I'm gonna first first I wanna use em all up .

[!]HOWEVER if there are more than one pair of '[]' then another problem
occurs.
Eg:
Input: I'm gonna first [//] second [//] third I wanna use em all up.
Output: I'm gonna first third I wanna use em all up.

* as u can see the 2nd 'first' is missing. This is because of
the "greediness" of Perl which tries to match as much of the search
pattern as possible.

To solve this, we use the '?' operator. Thus, the correct search pattern
is
$_ =~ s/\[.*?\]//;
This will give the output: I'm gonna first second [//] third I wanna use
em all up.

To remove all such occurrences, use the global search:
$_ =~ s/\[.*?\]//g;
Use a similar approach for '()'.

Regards,
Adarsh




------=_NextPart_000_0001_01C8A502.EC934050--

sorting array of array of array elements

am 23.04.2008 03:10:53 von gulsuner

Hi All,

I am trying to sort some data, which originally came as an excel file.

There are main titles, sub titles, sub sub titles and sub sub sub
titles. They defined by using different levels of indents in the
original file. I parse it and convert indents into spaces.

Main titles are sorted by first letters, but sub titles are not.

I need to sort all of them in their own groups as in the following
example:

(I put comments starting with an asterisk to make my question more
clear.)

------------------------------
Original file:

0 Apple
0 Book
0 Color
1 Red
1 Green
2 Tree
0 School
1 Collage
1 University
2 Zakulty
2 Faculty
3 Maculty
3 Aculty
2 Bus
1 Elementary

Output that I need.

0 Apple
0 Book
0 Color
1 Green * G < R
2 Tree
1 Red
0 School
1 Collage
1 Elementary * E < U
1 University
2 Bus * B < F|Z
2 Faculty
3 Aculty * A < M
3 Maculty
2 Zakulty
--------------------------------

I am trying to put them into arrays and try cmp $a<=>$b.

12 while (<$file>) {
13 chomp();
14 my ($tab, $title) = split ("\t", $_);
15 if ($tab eq "0") {
16
17 $main[$f] = $title;
18 $f++;
19 $g = "0";
20
21 }
22 elsif ($tab eq "1") {
23 $sub[$f][$g] = $title;
24 $g++;
25 $h = 0;
26 }
27 elsif ($tab eq "2") {
28 $sub2[$g][$h] = $title;
29 $h++;
30 $i = 0;
31 }
32 elsif ($tab eq "3") {
33 $sub3[$h][$i] = $title;
34 $i++;
35 }
36 }



this strategy works for only if there are main and sub titles. it
doesn't work for 2. and 3. level sub titles, because of the change of 1.
level sub title array.

"putting them into different files with a coding to indicate their upper
level titles, and later put them into arrays like
push ( @{$sub[3]}, (Red, Green) )
order them, and use this coding to put them under main titles" might be
helpful . But it will be confusing to me.

main.txt:
1 Apple
2 Book
3 School

sub.txt
3 1 Collage
3 2 University

sub2.txt
3 2 1 Faculty


or not flat files but using mysql with perl? ( one table with cols for
each sub level, 'if's and for loops with ORDER BY of Mysql ).
3 0 0 0 School
3 2 0 0 University
3 2 1 0 Faculty




So, have you got an idea or is there a known way to make this task
easier?



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Joining/Merging AoA

am 24.04.2008 15:53:25 von daggerquill unknown

T24gVHVlLCBBcHIgMjIsIDIwMDggYXQgMjoyOCBBTSwgVmlzaGFsIEcgPHYz Z3VwdGFAZ21haWwu
Y29tPiB3cm90ZToKPiBIaSBHdXlzLAo+Cj4gIEkgaGF2ZSBhIGxpdHRsZSBj b21wbGljYXRlZCBw
cm9ibGVtLi4uCj4KPiAgSSBoYXZlIHR3byBhcnJheXMKPgo+ICBAYSA9ICgg WydpZCcsICduYW1l
JywgJ2FnZSddLAo+ICAgICAgICAgICAgWycxJywgJ0ZyZWQnLCAnMjQnXSwK PiAgICAgICAgICAg
IFsnMics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XJzdCBlbGVtZW50OgoKCiAgICBwdXNoIG15IEBjLCBbQHtzaGlm dCBAYX1dOwogICAg
cHVzaCBAeyRjWzBdfSwgKEB7c2hpZnQgQGJ9KVsxXTsKCiAgICAkY1skXy0+ WzBdXSA9IFsgQHsk
X30gXSB3aGlsZSAkXyA9IHNoaWZ0IEBhOwogICAgcHVzaCBAeyRjWyRfLT5b MF1dfSwgJF8tPlsx
XSB3aGlsZSAkXyA9IHNoaWZ0IEBiOwoKCkhUSCwKCi0tIGpheQotLS0tLS0t LS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQpUaGlzIGVtYWlsIGFu ZCBhdHRhY2htZW50
KHMpOiBbIF0gYmxvZ2FibGU7IFsgeCBdIGFzayBmaXJzdDsgWyBdCnByaXZh dGUgYW5kIGNvbmZp
ZGVudGlhbAoKZGFnZ2VycXVpbGwgW2F0XSBnbWFpbCBbZG90XSBjb20KaHR0 cDovL3d3dy50dWF3
LmNvbSBodHRwOi8vd3d3LmRvd25sb2Fkc3F1YWQuY29tIGh0dHA6Ly93d3cu ZW5nYXRpa2kub3Jn
Cgp2YWx1ZXMgb2Yg4iB3aWxsIGdpdmUgcmlzZSB0byBkb20hCg==

RE: Joining/Merging AoA

am 24.04.2008 16:48:02 von Andrew Curry

As this is a beginners list are you going to explain...

push my @c, [@{shift @a}];
push @{$c[0]}, (@{shift @b})[1];

$c[$_->[0]] =3D [ @{$_} ] while $_ =3D shift @a;
push @{$c[$_->[0]]}, $_->[1] while $_ =3D shift @b;
=20


-----Original Message-----
From: Jay Savage [mailto:daggerquill@gmail.com]=20
Sent: 24 April 2008 14:53
To: Vishal G; Perl List
Subject: Re: Joining/Merging AoA

On Tue, Apr 22, 2008 at 2:28 AM, Vishal G wrote:
> Hi Guys,
>
> I have a little complicated problem...
>
> I have two arrays
>
> @a =3D ( ['id', 'name', 'age'],
> ['1', 'Fred', '24'],
> ['2', 'Frank', '42'],
> );
>
> @b =3D ( ['id', 'sex'],
> ['1', 'm' ],
> ['2', 'm'],
> );
>
> I want to join these two AoA, based on id, so the resulting array=20
> will look like this
>
> @c =3D ( ['id', 'name', 'age', 'sex'],
> ['1', 'Fred', '24', 'm' ],
> ['2', 'Frank', '42', 'm'],
> );
>
> Any Ideas?
>

It really depends on the data. Are you certain that both lists will be =
in
the same order? Are you certain that all data will exist for each =
person? If
so you can just loop though both lists. That's not very robust, though,
because as soon as you get a value that is one list and not the other,
everything gets out of sync.

I'd probably do something like the following, which stores the is in =
the
array index, as well as the first element:


push my @c, [@{shift @a}];
push @{$c[0]}, (@{shift @b})[1];

$c[$_->[0]] =3D [ @{$_} ] while $_ =3D shift @a;
push @{$c[$_->[0]]}, $_->[1] while $_ =3D shift @b;


HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] =
private and
confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com =
http://www.engatiki.org

values of =E2 will give rise to dom!


This e-mail is from the PA Group. For more information, see
www.thepagroup.com.

This e-mail may contain confidential information. Only the addressee =
is
permitted to read, copy, distribute or otherwise use this email or any
attachments. If you have received it in error, please contact the =
sender
immediately. Any opinion expressed in this e-mail is personal to the =
sender
and may not reflect the opinion of the PA Group.

Any e-mail reply to this address may be subject to interception or
monitoring for operational reasons or for lawful business practices.





--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Joining/Merging AoA

am 24.04.2008 17:49:36 von daggerquill unknown

QW5kIGFzIHRoaXMgaXMgdGhlIFBlcmwgYmVnaW5uZXJzIGxpc3QsIHBsZWFz ZSBkb24ndCB0b3Ag
cG9zdC4KCjIwMDgvNC8yNCBBbmRyZXcgQ3VycnkgPGFuZHJldy5jdXJyeUBw YS1zcG9ydC5jb20+
Ogo+IEFzIHRoaXMgaXMgYSBiZWdpbm5lcnMgbGlzdCBhcmUgeW91IGdvaW5n IHRvIG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