in reply to counter of files? something else?

Thank you all for your feedback. I really appreciate it. Let me be a bit more clear (hopefully..) about what I want to do.

Here is a part of what I've written so far

#count the files in my foreach loop (I'm in a foreach loop, #not shown, that loops through @files my $count = @files; my @count = (); # I'l explain what I want to do with this - Note 1. push (@count, $count); print $count; print "\n"; #now this is where I am creating my new files, getting data from the #files in the foreach loop my $pdb; open (my $pdb, '>', "$filename.pdb"); my $p; my $w; my $title = "$filename"; my $bx = 50.000; my $by = 50.000; my $bz = 50.000; printf $pdb "%s$title"; printf $pdb "\n"; printf $pdb ("%5d",$atomcount); printf $pdb "\n"; for ($p=1; $p<=$atomcount; $p++) { my $carboxyl = "@count[$w]"; #here is all that I want to do-Note 2 printf $pdb("%5d%-5s%5s%5d%8.3f%8.3f%8.3f",1,$carboxyl,@atom[$p],$ +p,@x[$p],@y[$p],@z[$p]); printf $pdb "\n"; } printf $pdb("%10.5f%10.5f%10.5f",$bx,$by,$bz); printf $pdb "\n"; close $pdb/;

Note 1: My idea is that I'll first count the files I'm looping through (they're 40) and then I'll be able to tell perl to show me the first file (that is file 1), then the second file (file 2), all the way until 40. That's why I'm putting $count in an array, in order to tell perl "ok now show me the first time you find this type of file; then the second time and so on". That way... (takes us to note 2)

Note 2: ...I will be able to get this $carboxyl entry unique every time. In the first pdb file it will be equal to 1, then 2, up to 40. So I will never have two same $carboxyl values in any of my pdb files. That way I will be able to distinguish between them by looking at the contents: the first one will have $carboxyl = 1 all the way down (250 lines), the second one will have $carboxyl = 2 all the time and so on.

The problem is that I'm doing it wrong because obviously I'm just printing the actual count number. Also I think the approach is wrong, because it might be better to actually assign numbers to $carboxyl based on how many pdb files I open and create, that would make more sense, right? Rather than how many files I loop through to get data FOR the pdb file I'll create later.

Am I at least getting close in terms of understanding this? I know I have to do reading - I already have two books (Learning Perl and Beginning Perl) and opened all the links you've recommended (in order to read, not just to close them back). I'm panicking though because this will take me days to correct - been trying since yesterday for a simple counter thing :'(

Replies are listed 'Best First'.
Re^2: counter of files? something else?
by soonix (Chancellor) on Apr 23, 2015 at 13:19 UTC
    • Does @files change during the loop? otherwise, you'll end up with $count having always the same value.
    • if you are inside a loop, the my variables are only for that one iteration. Is this what you want?
    • $count is not a very good variable name
    I think you want:
    • before the foreach loop: my $file_number = 0;
    • in the loop: $file_number += 1;
    Plus you seem to get the sigils wrong - see the first part of → perldata:
    @whatever is an array, a single element of that array is $whatever[123]. @whatever[123] is an array slice.

      - No, @files doesn't change through the loop.

      - That's a good point. I'll try it differently.

      - Ok, thank you for the suggestion.

      I'll try the two final bullet points!

      As for arrays/elements/slices: thank you, I normally get them right, I mixed it up a bit when trying to explain myself.

Re^2: counter of files? something else?
by Random_Walk (Prior) on Apr 23, 2015 at 13:29 UTC

    I am not sure why you are worried so much about counting files. If you have an array of files, perl will happily skip though them, one at a time, in order, using for. There is lots of stuff here though, that is not accounted for. You are not opening any files to read, $atomcount is a mystery. And the @atom array (probably better used at $atom[$p]) is also unmentioned before now.

    But anyway, here is a go at wrangling your code a little. It probably won't compile, but may give you some useful hints. If you can post a more complete listing of your code, we may be able to help more

    my @files = qw(file01 file02 file03 file04 file05); my $carboxyl = 0; # you may want to start at 1 instead for my $filename (@files) { open my $pdb, '>', "$filename.pdb"; my ($p, $w); my $bx = 50; # Never used again ? my $by = 50; # Never used again ? my $bz = 50; # Never used again ? print $pdb "\t%filename\n"; printf $pdb "%5d\n", $atomcount; for my $p (1 .. $atomcount) { # Perl likes to help printf $pdb "%5d%-5s%5s%5d%8.3f%8.3f%8.3f\n", 1,$carboxyl,@atom[$ +p],$p,@x[$p],@y[$p],@z[$p]; } $carboxyl++; printf $pdb "%10.5f%10.5f%10.5f\n",$bx,$by,$bz; close $pdb; }

    Update

    is 'pdb' http://en.wikipedia.org/wiki/Protein_Data_Bank_(file_format) ?

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      I want the files counted as I think of it as the only way to get different $carboxyl entries. All I want is just a number, from 1 to whatever, next to my $carboxyl entry, so that I will be able to tell the files apart.

      This number doesn't have to be associated with the files, it doesn't have to be a file counter, it just has to be consecutive and different every time, for each of the resulting pdb files.

      It's just because I'm a newbie that I thought that associating my goal with a file counter would make more sense and would be easier to implement.

      $atomcount and @atom aren't unaccounted for, they're just further up in my script (sorry about that, I didn't know how to include a concise part without copying the whole thing). Also, I'm opening the pdb files and printing coordinates (x,y,z) in them, from the files that are already open (again, opened further up in my script and these are the files of the foreach loop)

      Thank you (all) for you help. I'll try your suggestions and update you, hopefully I'll get it right - at least in my mind or on paper.

      Update: Yes, pdb is protein data bank files. Although I'm an idiot and instead of pdb it should read gro, as I changed to gro files (and thus the format is wrong for pdb files). I'm really sorry about that, thankfully it doesn't change my question though so not a disaster (I'm embarrassed)