By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

regarding grep function

P: 72
print Dumper(@data);

_____________
$VAR1= '2008,Car,Medium1,5000';
$VAR2= '2008,Car,Medium2,5000';
$VAR3= '2007,Plane1,Medium1,20000';
$VAR4= '2006,Boat,Small1,20000';
$VAR5= '2006,Plane2,Small1,20000';
$VAR6= '2006,Plane1,Small1,20000';
....
_______________



Expand|Select|Wrap|Line Numbers
  1. foreach $Year (sort @Type) {
  2. @NewData = grep(/^$Year/, @Data);
  3. $TotalPlane = grep(/Plane*/,grep(/Medium*/, @NewData));
  4. push(@gSum_Total, join(",",$Year, $TotalPlane));
  5. }
  6. print @gSum_Total;
  7. exit;
i wish to get the total number of plane using the grep function, .. but it wont work for me... as it sum up all the quantity.
is it grep only can be used to grep the first element??
Mar 26 '08 #1
Share this Question
Share on Google+
3 Replies


P: 72
another question.. could the wildcard (*) use for the quantifying match for number??
eg.. Plane1,Plane2

grep(/Plane*/,@Data);
Mar 26 '08 #2

rajiv07
100+
P: 141
Expand|Select|Wrap|Line Numbers
  1. my @array=('2008,Car,Medium1,5000','2008,Car,Medium2,5000','2007,Plane,Medium1,20000','2006,Boat,Small1,20000','2006,Plane2,Small1,20000','2006,Plane1,Small1,20000'); 
  2.  
  3.  my $year=2007;
  4.  
  5. my $totalplane=grep(/(^$year,Plane[0-9]*,Medium[0-9]*)/,@array);
  6.  
  7. print $totalplane;
Try this code, and the * is quantifier operator.Read this Help

Hope it help.

Regards
Rajiv
Mar 26 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
In a regexp this:

Expand|Select|Wrap|Line Numbers
  1. /Plane*/
means the pattern "Plan" followed by zero or more "e". It is not a wild card match.
Mar 26 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.