b_rimes All American 2072 Posts user info edit post |
open(INFO, $file); # Open the file while($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";
What I am trying to do is read a line from a file, read each character from the back to front, and stopping when I reach a '/' - which is the first character in each string(without a doubt).
It's successfully reading in multiple lines in the file, but it's just stopping at some point. Am I doing this incorrectly, and if so what is so screwed up?10/18/2005 9:42:56 PM |
confusi0n All American 5076 Posts user info edit post |
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 |
1CYPHER Suspended 1513 Posts user info edit post |
Learn $_ 10/18/2005 11:43:16 PM |
confusi0n All American 5076 Posts user info edit post |
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 |
1CYPHER Suspended 1513 Posts user info edit post |
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;
We take advantage that perl is greedy and will match multiple '/' all the way up to the last one. We capture the information you want in $1 with the parentheses, then print it reversed, count it, whatever.
If I were on campus I would test this for you, I got rid of my 'nix box a long time ago.
[Edited on October 18, 2005 at 11:50 PM. Reason : had to learn the code tag]10/18/2005 11:49:41 PM |
Excoriator Suspended 10214 Posts user info edit post |
open(INFO, $file); @lines = <INFO>; close(INFO); chomp(@lines);
foreach $line (@lines){ @words = split(///, $line); foreach $word (@words){ print reverse($word); } }
or thereabouts. I don't remember if the '/' char needs to be escaped - if so, just make the rex /\// and i've never used reverse so i'm just guessing at its syntax
If you post a testcase and sample output, i'd test it for you for like 5 min.
[Edited on October 18, 2005 at 11:52 PM. Reason : s]10/18/2005 11:49:41 PM |
1CYPHER Suspended 1513 Posts user info edit post |
Yea the slash has to be escaped like I did it. 10/18/2005 11:55:36 PM |
confusi0n All American 5076 Posts user info edit post |
err matthew you're not counting characters 10/18/2005 11:56:12 PM |
Excoriator Suspended 10214 Posts user info edit post |
or you could do this:
$/ = "/"; open(INFO, $file); @lines = <INFO>; close(INFO);
foreach $line (@lines){ print reverse($line); }
who said i have to count the chars and which chars am i supposed to count? each string or all of them combined? I'm still not sure exactly what this guy wants...
[Edited on October 18, 2005 at 11:58 PM. Reason : s]10/18/2005 11:57:03 PM |
confusi0n All American 5076 Posts user info edit post |
its perl there are literally infinity -1 ways to do anything
i think the only thing we can agree on is that the OP's code sucks balls 10/19/2005 12:00:02 AM |
Excoriator Suspended 10214 Posts user info edit post |
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 |
confusi0n All American 5076 Posts user info edit post |
agreed....i recall when techtalk was actually interesting
instead of e-panis swordfighting 10/19/2005 12:07:05 AM |
b_rimes All American 2072 Posts user info edit post |
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 +Text
The 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 |
Excoriator Suspended 10214 Posts user info edit post |
($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
then operate on that file - eh there are a million ways to do this - i was just trying to show you how to start thinking about the problem from the terminal rather than a script... i'm sure you could pipe it to another command that would count the '+' chars per line, but i'm not sure what at the moment
[Edited on October 19, 2005 at 11:05 AM. Reason : s]10/19/2005 11:01:54 AM |
b_rimes All American 2072 Posts user info edit post |
^ 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 |
Excoriator Suspended 10214 Posts user info edit post |
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 script
since 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 |
b_rimes All American 2072 Posts user info edit post |
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 |
lilbirdey Starting Lineup 55 Posts user info edit post |
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"; }
[Edited on October 19, 2005 at 12:43 PM. Reason : stupid smiley]10/19/2005 12:41:40 PM |
NutGrass All American 3695 Posts user info edit post |
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 |
b_rimes All American 2072 Posts user info edit post |
^ troll
So 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";
Does this look correct? Thanks to all for their help.
[Edited on October 19, 2005 at 10:33 PM. Reason : smiley]10/19/2005 10:32:15 PM |
Excoriator Suspended 10214 Posts user info edit post |
first of all, you shouldn't chop after chomp 10/20/2005 10:59:26 AM |
Excoriator Suspended 10214 Posts user info edit post |
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 |
1CYPHER Suspended 1513 Posts user info edit post |
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 |
Excoriator Suspended 10214 Posts user info edit post |
Quote : | "The idea that an array in scalar context gives the number of elements in the array isn't necessarily intuitive." |
i agree, but after seeing it in 4 people's code, it should have been somewhat easier to understand than if he just happened across it in a perl book
Quote : | "I mean he did attempt to use $_ which isn't bad for a rookie." |
yes, but I fear that he only did it because it seemed to him to be the corollary to C++'s cin .... i mean, even his use of $_ is very c++ish if you look at it that way
My only point is - why write perl if you're going to make the code as complex and cumbersome as C++...
[Edited on October 20, 2005 at 2:08 PM. Reason : s]10/20/2005 2:03:46 PM |
qntmfred retired 40810 Posts user info edit post |
gotta start somewhere. give the guy a break 10/20/2005 4:11:55 PM |
Excoriator Suspended 10214 Posts user info edit post |
i'm not being hard on him 10/20/2005 4:19:55 PM |
b_rimes All American 2072 Posts user info edit post |
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 |
Excoriator Suspended 10214 Posts user info edit post |
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 |
Excoriator Suspended 10214 Posts user info edit post |
*what parts 10/20/2005 11:19:42 PM |