Solved

Perl code to read from a file

Posted on 2009-07-06
7
154 Views
Last Modified: 2012-05-07
Hello, I have a small stats project to work on.

I am learning perl, but have to learn it in a very short space of time.

Could anyone help me with some code, which would open a text file, draw out 7 comma delineated numbers, ie, 34,56,76,12,1,69,64 and insert them as individual entities into scalar variables ?

thanks

Mick
0
Comment
Question by:MichaelGlancy
  • 4
  • 3
7 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24789471
Here is one way.
#!/usr/bin/perl
 

open(IN, "<test.txt") or die;
 

my $s;
 

while($s = <IN>) {

   if($s =~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {

      my ($n1, $n2, $n3, $n4, $n5, $n6, $n7) = ($1, $2, $3, $4, $5, $6, $7);

      print "n1 = $n1,  n7 = $n7\n";

   }

}

Open in new window

0
 

Author Closing Comment

by:MichaelGlancy
ID: 31600371
thankyou
0
 

Author Comment

by:MichaelGlancy
ID: 24789497
it doesn't print to the command line screen, should it ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24789514
You are welcome.

Also look at the "split" function, you can split comma separated lines easily with it. I often prefer an explicit regular expression,


my @nums = split /,/, "1,2,3,4,5,6,7"

#!/usr/bin/perl
 

open(IN, "<test.txt") or die;
 

my $s;
 

while($s = <IN>) {

   my @nums = split /,/, $s;

   foreach (@nums) {

      print "$_\n";

   }

}

Open in new window

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24789520
>>it doesn't print to the command line screen, should it ?

Yes, with the test data I used. I made a file test.txt with one line of text:  34,56,76,12,1,69,64

0
 

Author Comment

by:MichaelGlancy
ID: 24789523
I am using windows, should this make a difference ?
0
 

Author Comment

by:MichaelGlancy
ID: 24789532
I apologise, it did that time.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now