Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

reading var from the file

by perlb (Initiate)
on May 17, 2008 at 15:16 UTC ( [id://687098]=perlquestion: print w/replies, xml ) Need Help??

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

I have the Temp varaible store in a file. It's value ends with the number and a slash (/) at the end. Some its value is store in one line and some time it is store in 2 line with carriage return at he end of first line

- Temp: abds.wewe/fssaf:erferr@abc.co
c.us:342/
- Temp: abds.wewe/fssaf:erferr@abc.coc.us:342/

Does any one know how do I can get this value in a varaible...

Below code only gets the first line only, I want to get the value where its end with "/".
sub matchAttribute { $pos = index($_, " "); $var11 = substr($_, $pos); $var11 =~ s/^\s+//; return $var11; } while loop if (/^Temp:/){ chomp; $var_temp = matchAttribute ($_); print "var_temp= $var_temp"; }
Current Output:
var_temp= abds.wewe/fssaf:erferr@abc.co
Expected Out put:
var_temp= abds.wewe/fssaf:erferr@abc.coc.us:342/
Does any one has an idea how to do this...
Thanks,

Replies are listed 'Best First'.
Re: reading var from the file
by cdarke (Prior) on May 17, 2008 at 15:32 UTC
    The trick is to recoginse that the records do not just end in "\n", but in "/\n":
    use strict; use warnings; local $/ = "/\n"; while (my $var_temp = <DATA>) { $var_temp =~ s/\n//g; # Strip out new-lines print "$var_temp\n"; } __DATA__ abds.wewe/fssaf:erferr@abc.co c.us:342/ abds.wewe/fssaf:erferr@abc.coc.us:342/
    This gives :
    abds.wewe/fssaf:erferr@abc.coc.us:342/ abds.wewe/fssaf:erferr@abc.coc.us:342/
Re: reading var from the file
by mwah (Hermit) on May 17, 2008 at 16:00 UTC

    if cdarke's method doesn't work (if the end of the record /\n is not always consistent), you may parse the whole file the "hard way":

    use strict; use warnings; my $regexp = qr{ Temp: # hit => the temp: keyword (.+?) # capture what follows (?: # until (?=[\s-]+Temp) # another temp: shows up | # or \s*\z # the end of the data is here ) }msx; open my $fh, '<', 'tmpvar.dat' or die "wtf, $!"; { local $/; # slurp the entire file in +to my $content = <$fh>; # a buffer while($content =~ /$regexp/g) { # and extract each temp: r +ecord (my $var_temp = $1) =~ tr/\r\n//d; # delete new line (if any) print "var_temp=$var_temp\n" # and print } } close $fh;

    Regards

    mwa

Re: reading var from the file
by pc88mxer (Vicar) on May 17, 2008 at 15:21 UTC
    Update: I think I initially misread what you what to do, so here's a different answer.

    It seems you want to collect lines until the next line that begins with Temp:. The pattern for this is:

    my $temp; while (<>) { chomp; if (s/^Temp:\s*//) { if (defined($temp)) { ...use $temp... }; $temp = $_; } else { $temp .= $_; } } if (defined($temp)) { ...use $temp... }

    My original answer:

      Input file also an other varaible which ends on the one line. Only one varialbe has its value store in 2 lines ....

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://687098]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found