in reply to split file and put contents in variables?

Hi all, thanks for your effort! I managed to do this, which I think does what I want:

if (-e "test.txt") { open (my $test, '<', "test.txt") or print "Can't open file: $!"; print "opening file \n"; while ( my $line = <$test> ) { my ($a, $b, $c) = split / /, $line; print "$a, $b, $c \n" ; } }

What do you think? I've replaced my variable names with $, $b, $c for testing, just to be quicker.

But I found that I have to match 5 whitespaces, so I was wondering if I can do that with a /d{5}/ or something like that? Instead of matching for / / which needs the spacebar pressed 5 times and doesn't look as nice?

Replies are listed 'Best First'.
Re^2: split file and put contents in variables?
by davido (Cardinal) on Feb 02, 2016 at 15:40 UTC

    The split pattern behaves mostly like a regular expression, with the differences well documented in split. There is no such thing as a "d" meta-character-class. But there is \d. Your example of /d{5}/ would work if you just used proper metasymbol escaping: /\d{5}/.

    And any quantifier that is valid for regular expressions would be valid for the pattern used in split. If you wish, you might use +, {0,5}, {5,}, and so on.


    Dave

Re^2: split file and put contents in variables?
by toolic (Bishop) on Feb 02, 2016 at 15:37 UTC
    What do you think?
    I think you should use the code I showed :)
    my ($x, $y, $z) = split /\s+/, $line;
    • \s+ grabs a variable number of spaces: 1, 5, whatever. This is documented in the link I showed you: split.
    • $a and $b have special meaning to sort. Don't use them here. You should probably use more meaningful names as well.

      Thanks toolic :)

      I have one more question, I just remembered actually that the first entry in my file, namely FOLDING, is made up from two variables, as in I have assigned it to two variables, $pattern and $match, used as $pattern$match. I know it looks weird but I needed to do it that way.

      So if in a different file my FOLDING pattern is "W" and matches a lipid, I would have an entry as "Wlipid". Hopefully that makes sense?

      So my question is how do I match this now? Do I just match is as my ($w$x, $y, $z) ? Or it makes no sense?

        I may have got this completely wrong but it looks to me like you want to match lines in the file to some previously assigned variables. If so, perhaps this will clarify

        #!perl use strict; use warnings; my $pattern = 'W'; my $match = 'lipid'; my $cyclo = 'CC'; my $group = 'GGG'; my $results = 'c:/temp/'; my $filepath = 'Test/'; my $infile = 'ABC.txt'; chdir "$results$filepath" or die "cannot chdir to $results$filepath ! $!"; if (-e $infile) { open my $test, '<', $infile or die "Can't open $infile: $!"; while (my $line = <$test>){ my @f = split ' ', $line; if ( ($f[0] eq $pattern.$match) && ($f[1] eq $cyclo) && ($f[2] eq $group) ){ print $line; } } } else { print "$infile does not exist"; }
        poj
Re^2: split file and put contents in variables?
by choroba (Cardinal) on Feb 02, 2016 at 15:48 UTC
    The single space string ' ' (not a regex / /!) I suggested is special in split, as documented. It behaves as /\s+/, but it's shorter and more readable.
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^2: split file and put contents in variables?
by ww (Archbishop) on Feb 03, 2016 at 12:38 UTC

    Moeover, don't get into the bad habit of using "$a" for anything other than sort, q.v.   and even more emphatically, do use meaningful names for $var.

    That will cost you a very small bit of extra effort when writing the program... and save you or some future maintainer a huge PITA when trying to understand or modify your program some-when in the distant future... like, say, next week.


    Come, let us reason together: Spirit of the Monastery