Regex
--0015174c3f8c304845049aca8785
Content-Type: text/plain; charset=UTF-8
I am trying to extract numeric values from a data file and I'm having
trouble writing a regular expression that would do this.
My data looks like this: <{5, 26}{20, 42, 64}{23, 48}>
I am trying to turn this into a list of lists [ [5, 26], [20, 42, 64],
[23,48] ]
Any pointers would be greatly appreciated
--0015174c3f8c304845049aca8785--
Re: Regex
--0-1211532990-1296096992=:74439
Content-Type: text/plain; charset=us-ascii
Is this what you're trying to do Greg? Try running the script below:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
my $data = "<{5,26} {20,42,64} {23,48}>"; # assuming it's read in from a
file
$data =~ s/(?<=})\s*(?={)/,/g; # replace spaces between number sets with commas
$data =~ s/[<{]/[/g; # replace < and { with [
$data =~ s/[}>]/]/g; # replace > and } with ]
my $list_of_list = eval $data; # we get an anonymous list of list
# DISPLAY the list of list
# ========================
# use dumper to print out list of list
print Dumper $list_of_list;
# or the step by step method
for my $outerlist ( [at] $list_of_list) {
for my $innerlist ( [at] $outerlist) {
print $innerlist . " ";
}
print "\n";
}
________________________________
From: Greg J <greg.jarzab [at] gmail.com>
To: beginners [at] perl.org
Sent: Thu, January 27, 2011 10:15:48 AM
Subject: Regex
I am trying to extract numeric values from a data file and I'm having
trouble writing a regular expression that would do this.
My data looks like this: <{5, 26}{20, 42, 64}{23, 48}>
I am trying to turn this into a list of lists [ [5, 26], [20, 42, 64],
[23,48] ]
Any pointers would be greatly appreciated
--0-1211532990-1296096992=:74439--
Re: Regex
At 8:15 PM -0600 1/26/11, Greg J wrote:
>I am trying to extract numeric values from a data file and I'm having
>trouble writing a regular expression that would do this.
>
>My data looks like this: <{5, 26}{20, 42, 64}{23, 48}>
>
>I am trying to turn this into a list of lists [ [5, 26], [20, 42, 64],
>[23,48] ]
>
>Any pointers would be greatly appreciated
I would use the Text::Balanced module to extract the characters
between braces and the split function to extract the numbers
separated by commas.
perldoc Text::Balanced
perldoc -f split
--
Jim Gibson
Jim [at] Gibson.org
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regex
On 27/01/2011 02:15, Greg J wrote:
> I am trying to extract numeric values from a data file and I'm having
> trouble writing a regular expression that would do this.
>
> My data looks like this:<{5, 26}{20, 42, 64}{23, 48}>
>
> I am trying to turn this into a list of lists [ [5, 26], [20, 42, 64],
> [23,48] ]
>
> Any pointers would be greatly appreciated
Hi Greg
I am tempted to write
my $data = '<{5, 26}{20, 42, 64}{23, 48}>';
my $list = [ map { [ $_ =~ /\d+/g ] } $data =~ /(\{.*?\})/g ];
But the program below seems to do what you described more clearly :)
HTH,
Rob
use strict;
use warnings;
my $data = '<{5, 26}{20, 42, 64}{23, 48}>';
my $list;
while ($data =~ /(\{.*?\})/g) {
my [at] sublist = $1 =~ /\d+/g;
push [at] $list, \ [at] sublist;
}
use Data::Dumper;
print Dumper $list;
**OUTPUT**
$VAR1 = [
[
'5',
'26'
],
[
'20',
'42',
'64'
],
[
'23',
'48'
]
];
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regex
--0-19653684-1296109423=:77158
Content-Type: text/plain; charset=us-ascii
From: Rob Dixon <rob.dixon [at] gmx.com>
To: beginners [at] perl.org
Cc: Greg J <greg.jarzab [at] gmail.com>
Sent: Thu, January 27, 2011 11:38:07 AM
Subject: Re: Regex
On 27/01/2011 02:15, Greg J wrote:
> I am trying to extract numeric values from a data file and I'm having
> trouble writing a regular expression that would do this.
>
> My data looks like this:<{5, 26}{20, 42, 64}{23, 48}>
>
> I am trying to turn this into a list of lists [ [5, 26], [20, 42, 64],
> [23,48] ]
>
> Any pointers would be greatly appreciated
Hi Greg
I am tempted to write
my $data = '<{5, 26}{20, 42, 64}{23, 48}>';
my $list = [ map { [ $_ =~ /\d+/g ] } $data =~ /(\{.*?\})/g ];
But the program below seems to do what you described more clearly :)
HTH,
Rob
use strict;
use warnings;
my $data = '<{5, 26}{20, 42, 64}{23, 48}>';
my $list;
while ($data =~ /(\{.*?\})/g) {
my [at] sublist = $1 =~ /\d+/g;
push [at] $list, \ [at] sublist;
}
use Data::Dumper;
print Dumper $list;
**OUTPUT**
$VAR1 = [
[
'5',
'26'
],
[
'20',
'42',
'64'
],
[
'23',
'48'
]
];
-- To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Hi Rob,
I refer to your 2 lines of code:
my $data = '<{5, 26}{20, 42, 64}{23, 48}>';
my $list = [ map { [ $_ =~ /\d+/g ] } $data =~ /(\{.*?\})/g ];
That's the type of Perl coding style I'm still trying to learn. Concise and
elegant. Beautiful!!!
I am having a tough time to crack the secret of Perl's contexts. Any
websites/books you could recommend which
covers this area in depth and clearly?
--0-19653684-1296109423=:77158--
Re: Regex
On 27/01/2011 06:23, Ramesh Kumar wrote:
>
> Hi Rob,
>
> I refer to your 2 lines of code:
>
> my $data = '<{5, 26}{20, 42, 64}{23, 48}>';
> my $list = [ map { [ $_ =~ /\d+/g ] } $data =~ /(\{.*?\})/g ];
>
> That's the type of Perl coding style I'm still trying to learn. Concise and
> elegant. Beautiful!!!
Code like that can be useful as an exercise as it tests your
understanding of Perl constructs quite well. But I would never recommend
that you write like that on a regular basis as it takes a /long/ time
for someone else to comprehend if they have to maintain your code. You
would even kick yourself for being so clever if you had to come back to
some of your own code at a later date and couldn't see its structure at
a glance. That is why I wrote code that works using a loop but achieves
the same result in a similar way.
It is worth pointing out that the one-liner is also inefficient with
memory, as the list generated by $data =~ /(\{.*?\})/g is created all at
once and then mapped, whereas the loop creates and processes a single
bracketed sublist at a time. It is also likely to be slower, although I
haven't benchmarked it.
Always write something that explains what it does and so needs minimum
commenting!
> I am having a tough time to crack the secret of Perl's contexts. Any
> websites/books you could recommend which
>
> covers this area in depth and clearly?
I think the section (2.7) on context in Programming Perl is clear and
helpful. And have you seen the tutorial on Perl Monks?
<http://www.perlmonks.org/?node_id=738558>
I also recommend playing with a subroutine that displays its called
context, like the program below. It lets you see the contexts that
different calls apply.
use strict;
use warnings;
sub context {
my $list_context = wantarray();
if (not defined $list_context) {
print "Void context\n";
}
elsif ($list_context) {
print "List context\n";
}
else {
print "Scalar context\n";
}
}
no warnings 'void'; # Don't warn about void calls
[ context() ];
context() & 1;
( context() );
**OUTPUT**
List context
Scalar context
Void context
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regex
On 2011-01-27 03:56, Ramesh Kumar wrote:
> my $list_of_list = eval $data; # we get an anonymous list of list
Looks like you didn't get the memo yet. :)
String eval is normally not what you want. I only use it in exceptional
cases.
At least first check the contents of $data. That normally leads to Perl
code that doesn't need eval at all anymore.
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Regex
On Thu, Jan 27, 2011 at 07:59:41PM +0000, Rob Dixon wrote:
> On 27/01/2011 06:23, Ramesh Kumar wrote:
> >
> >Hi Rob,
> >
> >I refer to your 2 lines of code:
> >
> > my $data = '<{5, 26}{20, 42, 64}{23, 48}>';
> > my $list = [ map { [ $_ =~ /\d+/g ] } $data =~ /(\{.*?\})/g ];
> >
> >That's the type of Perl coding style I'm still trying to learn. Concise and
> >elegant. Beautiful!!!
Maybe it doesn't belong in production code but I must agree with Mr. Kumar,
'Concise, elegant, beautiful.'
My hat's off to you.
Mike McClain
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/