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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

my $small_string = substr ($_, 168, 6); SUBSTR

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
US
Hello, First of all, I open a file in Perl and then i use substr to pull out just a piece of each row in my flat file (which is fixed length). Now, I would like to get a count of that substr - that actually has values, that isn't empty or blank.

So I'm not sure which function to use. Here is what I have so far. Could someone point me in the right direction? It would be so very helpful!

$file = 'somefile.txt';
open(INFO, $file) or die "Can't open ...";

while (<INFO>){
chomp;
my $small_string = substr ($_, 168, 6);
print "$small_string\n";
}

So essentially, I want to get a count of all the $small_string - that actually have a value. I am a NOVICE - so just a little point in the right direction would be so entirely helpful.

Best,

Polka
 
one possibility:

Code:
my $n = 0;
while (<INFO>){
  chomp;
  $n++ if (my $small_string = substr ($_, 168, 6));
  print "$small_string\n";
}
print "Number of small_strings with a value: $n";
 
I think there might be a correction to Kevins program.
Incase the small string has blank spaces and you dont want to count that either the following program shd work.

"Now, I would like to get a count of that substr - that actually has values, that isn't empty or blank"


my $n = 0;
while (<INFO>){
chomp;
my $small_string = substr ($_, 168, 6));
$n++ if ($small_string =~ /^\s*$/);
print "$small_string\n";
}
print "Number of small_strings with a value: $n";
 
Hi there! Thank you so much for your help. Yes, VJ Cyrano, there were a few that had blank spaces, so it was helpful to use your regular expression.

I have a file with 100 rows, and the perl script is telling me that 81 of my small_strings have values - (but by my manual count - there should only be 19), so I think I will need to narrow this down even further (maybe there are tabs or some sort of something in there in the data? I dunno yet, need to analyze ...)

I will let you know if I get this solved, also - let me know if you have other ideas!

Thank you,

P4E
Detroit, MI, USA
 
Error on my part.
it shd hav been

$n++ unless ($small_string =~ /^\s*$/);
not
$n++ if ($small_string =~ /^\s*$/);

Hence the count was for the empty strings which
were 81, 19 were the strings with data in them - total 100.



 
Using previous solutions; this will only do the counting.
Code:
my $count = grep { substr($_, 168, 6) !~ /^\s*$/ } <INFO>;
print "Number of small_strings with a value: $count\n";
 
AWESOME help. Thank you so much. You've all been extremely helpful.

~P4E
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top