--
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:
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.
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
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.
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/
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/