Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question about how to use grep 2

Status
Not open for further replies.

lcs01

Programmer
Joined
Aug 2, 2006
Messages
182
Location
US
I have a long string, e.g.

"...aUniqueString=(99)...aUniqueString=(836)...aUniqueString=(34789)..."

Where

1) 'aUniqueString=()' is fixed;
2) what in '()' is always a digit;
3) '...' can be anyting including the string 'aUniqueString'.

Could someone show me how to grep 'aUniqueString=(..)' and put them into an array like:

@myArray = ('aUniqueString=(99)', aUniqueString=(836)', 'aUniqueString=(34789)');

Thank you for your help.
 
You can do it without grep.
Code:
#!/usr/bin/perl
use strict;
use warnings;

my @array;

while (<DATA>) {
   chomp;
   push @array, (/aUniqueString=\((\d+)\)/g);
}

print join("\n", @array); 

__DATA__
aUniqueString=(34789)other_rubbishaUniqueString=(1)  spaces aUniqueString=(77)
aUniqueString=(123)other_rubbishaUniqueString=(0)  junk aUniqueString=(1234567890)
extracts only the numeric values within the brackets. No sense in storing the 'aUniqueString()' in the array; it's always the same. If it is unique, you can use a hash instead of an array.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Wow, I am amazed! Thank you so much.

However, please bear with me a little. I don't quite understand your syntax. And in my case, the input is not an array, but a string like this:

Code:
my $string = "aUniqueString=(34789)other_rubbishaUniqueString=(1)  spaces aUniqueString=(77)aUniqueString=(123)other_rubbishaUniqueString=(0)  junk aUniqueString=(1234567890)";

How can I modify your code to make it work?

Thank you for your advice!
 
Code:
my $string = "aUniqueString=(34789)other_rubbishaUniqueString=(1)  spaces aUniqueString=(77)aUniqueString=(123)other_rubbishaUniqueString=(0)  junk aUniqueString=(1234567890)";
my @array = $string =~ /aUniqueString=\(\d+\)/g;
print "$_\n" for @array;

- Kevin, perl coder unexceptional!
 
My input was a string, just not in the way you were expecting. I used the __DATA__ as a way of posting a working example that reads data from a 'file'.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Thank you, Steve and Kevin, so much!
 
I guess that I have to ask some questions again...

When I applied Kevin's code in my real work, everything runs just as expected. That means '@array' has only the numeric values within the brackets. I am happy with that.

However, when I run the following sample code, to my surprise, the output is different:

Code:
[lcs@sable 1222] => cat t1.pl
#! /usr/local/bin/perl

use strict;
use warnings;

my $string = "aUniqueString=(34789)other_rubbishaUniqueString=(1)  spaces aUniqueString=(77)aUniqueString=(123)other_rubbishaUniqueString=(0)  junk aUniqueString=(1234567890)";
my @array = $string =~ /aUniqueString=\(\d+\)/g;
print join("\n", @array);
print "\n\n";

[lcs@sable 1223] => ./t1.pl
aUniqueString=(34789)
aUniqueString=(1)
aUniqueString=(77)
aUniqueString=(123)
aUniqueString=(0)
aUniqueString=(1234567890)

[lcs@sable 1224] =>

Why doesn't '@array' contains only digits?

Thank you very much for your help.
 
Steve's code would return just the digits, mine returnms what you are getting. The difference is Steve included capturing brackets '()' in the regexp:


/aUniqueString=\((\d+)\)/g


see, it has: (\d+)

which returns that part of the match only. Mine has no such brackets:

/aUniqueString=\(\d+\)/g;

so returns the entire match. If you want just the digits use the regexp Steve posted.






- Kevin, perl coder unexceptional!
 
Wow! That's why!! Thank you! Thank you so much, Kevin, for your kind explanation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top