AmberThai has asked for the wisdom of the Perl Monks concerning the following question:

Hello friends, I am new to perl programming and I would like some help about reading files.

I wish to know how can you can do something like

open my $input, '<', $ARGV[0]; open my $output,'>', $ARGV[1]; while (my $sentence = <$input>) { my $substring = 'clothes.*?mall'; $sentence =~ read-only{$substring}; print $output , $sentence; }

and read from a text file that contains something like :"I used to buy clothes from the mall because they are cheap" only the values between the words "clothes" and "mall" so I could do certain operations with them and after I do all the operations with the values between the 2 keywords I want to output the file with all the values("I used to buy clothes' from the 'mall because they are cheap") the words between ' ' represent the words " from the " after the operations were done on them.

P.S. Sorry if it is a stupid question or a stupid example but I am super new in all this and a terrible question asker.

Replies are listed 'Best First'.
Re: Reading from files
by choroba (Cardinal) on Jan 18, 2016 at 09:42 UTC
    If the string between the two keywords doesn't contain newlines, you can simply use a regex:
    #!/usr/bin/perl use warnings; use strict; open my $IN, '<', shift or die $!; open my $OUT, '>', shift or die $!; while (<$IN>) { my ($before, $between, $after) = /(.*?clothes)(.*?)(mall.*)/; print {$OUT} "$before '$between' $after\n"; }

    ($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,

      and if you have something like :"abc clothes doing shopping at the .m abc something doing shopping at the .mabc" why does something like :

      use warnings 'all'; use strict; use autodie; open my $input, '<', $ARGV[0]; open my $out, '>', $ARGV[1]; while (<$input>) { my ($before, $between, $after) = /(.*?.clothes|.something +)(.*?)(\.m.*)/; print {$out} "$before $after"; }

      does not read the values :"abc abcabc" and after the explanations can you tell me how could I do this?

        The matching only happens once, unless you specify the /g modifier.

        I'd probably use look-arounds for what you need:

        #!/usr/bin/perl use warnings; use strict; use Syntax::Construct qw{ \K }; while (<DATA>) { s/ (?:clothes|something) # Starting keyword. \K # Don't include the previous part into th +e replacement. (.*?) # The part to be quoted. (?=\.m) # Followed by ".m". /'$1'/xg; print; } __DATA__ abc clothes doing shopping at the .m abc something doing shopping at t +he .mabc
        ($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: Reading from files
by Discipulus (Canon) on Jan 18, 2016 at 09:38 UTC
    Hello AmberThai and welcome to the monastery!
    If you are 'super new' i have some suggstion:

    • die unless $ARGV[0];die unless $ARGV[1]; make you sure to have what you 'll need after
    • open my $input, '<', $ARGV[0]; should always be open my $input, '<', $ARGV[0]  or die "Unable to open [$ARGV[0]]";
    • move  my $substring = 'clothes.*?mall'; outside of the loop: you are reinstatiate the variable each iteration
    •  my $substring = 'clothes.*?mall'; what is? you perhaps want a regex? a compiled one? see qr in quote like operator
    • what is read-only{$substring}? come from this module?
    • you probably want someting like print $output $sentence if $sentence=~/$compiled_regex_with_qr_operator/ (update removed the comma in $output , $sentence 'cause the first is a file handle. as spotted by Athanasius, thanks)


    HtH
    L*
    UPDATE: perlrun has many gifts ready to you! your achievement is pretty easy with a Perl onliner and a redirection (shell redirection)
    #cat cat.txt meow pretty woman arf #perl -lne "print if /ty wo/" cat.txt > cat.out.log #cat cat.out.log pretty woman
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.