open(INFO, $file); # Open the filewhile($line = <INFO>{ $temp = chomp($line); $number = 0; $slash = '/'; while($temp ne $slash){ $number++; $temp = chop($line); print $temp; } $lines++; }print "There were";print " in ";print $lines;print "\n";
10/18/2005 9:42:56 PM
what you're trying to do isn't clear but i think this is close
$slash = '/';$number=0;open(INFO, $file);@lines = <INFO>;close(INFO);foreach $line ( @lines ){ $line = chomp($line); $number += length($line) - rindex($line,$slash); print reverse(substr($line),$number--);}print "There were $number in $lines lines\n" ;
10/18/2005 11:24:35 PM
Learn $_
10/18/2005 11:43:16 PM
I didn't want to confuse him too badly with $_anyway code is much more readable and maintainable without it
10/18/2005 11:47:32 PM
You could also do something like
my $number = 0;my $lines = 0;open(INFO, $file);while <INFO> { $lines++; if(.*\/(.*)) { print reverse $1; $number = $number + _____ (whatever function counts string lengths, I don't know it off the top of my head) } else { # no slashes on this line of input, do whatever you want }}close INFO;
10/18/2005 11:49:41 PM
open(INFO, $file);@lines = <INFO>;close(INFO);chomp(@lines);foreach $line (@lines){ @words = split(///, $line); foreach $word (@words){ print reverse($word); }}
Yea the slash has to be escaped like I did it.
10/18/2005 11:55:36 PM
err matthew you're not counting characters
10/18/2005 11:56:12 PM
or you could do this:
$/ = "/";open(INFO, $file);@lines = <INFO>;close(INFO);foreach $line (@lines){ print reverse($line);}
10/18/2005 11:57:03 PM
its perl there are literally infinity -1 ways to do anythingi think the only thing we can agree on is that the OP's code sucks balls
10/19/2005 12:00:02 AM
its no fun being a perl n00b - i'm no wizard but i can parse my way around text files... its a bitch of a language to learn from scratch.but its nice to have a real thread to post in rather than some stupid bullshit about gadgets [Edited on October 19, 2005 at 12:03 AM. Reason : s]
10/19/2005 12:02:10 AM
agreed....i recall when techtalk was actually interestinginstead of e-panis swordfighting
10/19/2005 12:07:05 AM
Well since you guys like interesting tech talk so much . . .Here is the background. I have a HUGE text file - probably 20 Megs. The format of the file will always be 67 characters of garbage (that may or maynot contain a '+'), then a '/', then periodic '+'s mixed in with characters, numbers, and spaces. I want to count the number of '+'s contained on each line - unless it's in the first 67 characters. I thought working from back to front would be wise since there is no telling what characters are in the first 67 spots. Also, I can guarantee that the last '/' on each line will seperate the garbage from the good text. GA3aRB56AG9EGAsRBAG++EGARBAG8EGARBAGEGA23hR+BAGEGARGEGA23hR+BAGEGAR/+Text +TextThe above line should count that there are 2 +s. I just need the output to say:"There were 2 in 1 lines"Given the above requirements - is Perl still the best option, and do your above suggestions still hold?
10/19/2005 10:13:59 AM
($trash, $data) = split(/\//, $line, 2);if you're sure there will only be one '/' in the line just go to your terminal and say,
cat FILE | sed 's:/: :g' | awk '{print $2}' > newFILE
10/19/2005 11:01:54 AM
^ I'm not completely sure that would work, as there is no guarantee that there isn't a / in the first 67 characters (in the trash). That would cause an unintentional split.
10/19/2005 11:05:31 AM
oh alright. well ok.btw, what OS are you running? 20meg isn't that big really - shouldn't even take 2 seconds to process with a scriptsince you've clarified the '/' issue:@temp = split(/\//, $line);$data = $temp[@temp-1];[Edited on October 19, 2005 at 11:10 AM. Reason : s]
10/19/2005 11:06:51 AM
I'll be doing it on linux - problem is I don't know a whole lot about scripting (less than I do about perl. Back to your above suggestion - let's assume that the only place the characters /+ are seen are where the data is split. Is it possible to split the data using two characters together, and once the data is split how to I cound the number of +s?
10/19/2005 11:13:39 AM
First, in your original program and a few of the replies, chomp is used incorrectly."Unlike chop, comp returns the number of characters deleted." - so sayeth the perl manual. So "$temp = chomp($line)" will remove a single newline character from the end of $line, and it will set $temp to "1". Second, this code should do what you want:
while (my $line = <> ) { my @parts = split m#/#, $line; # keep the part after the last slash my $goodpart = $parts[-1]; # do a dummy substitution and count the matches my ($ct) = ($goodpart =~ s/\+/+/g); print "Found $ct plusses\n";}
10/19/2005 12:41:40 PM
b_rimes isn't 1337...i'll tell you one day how to write this out...ub3r d00rk!!!1
10/19/2005 4:45:12 PM
^ trollSo I finally figured it out, and it seems to be working correctly. Thought someone else might actually want to see it:
open(INFO, $file); # Open the file$lines=0;$number = 0;$slash = "/";$plus = "+";while(<INFO> ) { $lines++; $line = $_; #print $lines; chomp($line); $temp = chop($line); print $temp; while(($temp ne $slash) and (length($line) > 37)){ if($temp eq $plus){ $number++; } $temp = chop($line); } }print "There were ";print $number;print " in ";print $lines;print "\n";
10/19/2005 10:32:15 PM
first of all, you shouldn't chop after chomp
10/20/2005 10:59:26 AM
also, get out of your c++ mindset@lines = <INFO>;$numberOfLines = @lines;and for the love of god, learn the split command. i know it looks scary but really man - the way you've written that script you might as well have used C++I'll give you a pass on regular expressions, but it seems to me that you are afraid to learn @variables, split, and foreach. what say you to these charges[Edited on October 20, 2005 at 11:24 AM. Reason : s]
10/20/2005 11:22:23 AM
Obivously, if you are just trying to get out a trivial task, you aren't going to take an hour or two (or 5 for the slow learners) of your day to learn what really makes perl nice compard to C++.The idea that an array in scalar context gives the number of elements in the array isn't necessarily intuitive. If you read a book this little 'feature' will be one of tons of nice things perl does that some other language doesn't and that isn't exactly easy to digest in one afternoon.I mean he did attempt to use $_ which isn't bad for a rookie.
10/20/2005 2:00:11 PM
10/20/2005 2:03:46 PM
gotta start somewhere. give the guy a break
10/20/2005 4:11:55 PM
i'm not being hard on him
10/20/2005 4:19:55 PM
No worries - like I said, thanks for everyone's help. You're right, my background has been mainly in Java, and this was my first shot at a Perl program. I apprecaite the feedback on my programming skills and habits - it shows me where I could improve and what I don't yet understand in a language.
10/20/2005 9:14:18 PM
well parts of what we suggested were intimidating to you? I know how cryptic it all looks at first.
10/20/2005 9:25:35 PM
*what parts
10/20/2005 11:19:42 PM