How can I do something based on the first word of a line?

S

Steve

I have this chunk of code which is broken because I can't figure this
out. Basically I have a file handle, and the file it reads in always
starts wit ha number... 1-99. I want to ONLY get that number, and not
the rest of the line because I want to be able to copy that line that
amount of times.

while(<FH3>)
{
my $x = 0;
#Make enough records based on quantity.. broken par
if(/^[0-9]|^[0-9][0-9]/)
{
print m/^[0-9]|^[0-9][0-9]/;
}

#Check each line for a type
if(/SWITCH|MONITOR|ROUTER|KVM/)
{
print HARDWARE $_;
$x=1;
}
if(/LICENCE|LICENCES/)
{
print LICENSES $_;
$x=1;
}
if(/COMPUTER|LAPTOP|THIN/)
{
print COMPUTER $_;
$x=1;
}
if(/SOFTWARE/)
{
print SOFTWARE $_;
$x=1;
}
if(/OS|'OPERATING SYSTEM'/)
{
print OS $_;
$x=1;
}
if ($x == '0')
{
print "Warning, following line didn't match anything and was
appended to the 'other' file!\n $_\n";
print OTHER $_;
}
}
 
S

sln

I have this chunk of code which is broken because I can't figure this
out. Basically I have a file handle, and the file it reads in always
starts wit ha number... 1-99. I want to ONLY get that number, and not
the rest of the line because I want to be able to copy that line that
amount of times.

Where is the code that "copy that line that amount of times"?
while(<FH3>)
{
my $x = 0;
#Make enough records based on quantity.. broken par

What does this mean?
if(/^[0-9]|^[0-9][0-9]/)

This will only find the first digit.
{
print m/^[0-9]|^[0-9][0-9]/;

Don't need to do this again.
print "$1\n" if (/^(\d\d*)/)
}

#Check each line for a type

There must be a better way to do this.
Apparently each line could have multiple
items?

Untested:
my $x = 0;
while (/(SWITCH|MONITOR|ROUTER|KVM) |
(LICENCES?) |
(COMPUTER|LAPTOP|THIN) |
(SOFTWARE) /xg
) {
defined $1 && print HARDWARE $_;
defined $2 && print LICENSES $_;
defined $3 && print COMPUTER $_;
defined $4 && print SOFTWARE $_;
$x = 1;
)

if(/SWITCH|MONITOR|ROUTER|KVM/)
{
print HARDWARE $_;
$x=1;
}
if(/LICENCE|LICENCES/)
{
print LICENSES $_;
$x=1;
}
if(/COMPUTER|LAPTOP|THIN/)
{
print COMPUTER $_;
$x=1;
}
if(/SOFTWARE/)
{
print SOFTWARE $_;
$x=1;
}
if(/OS|'OPERATING SYSTEM'/)
{
print OS $_;
$x=1;
}
if ($x == '0')
{
print "Warning, following line didn't match anything and was
appended to the 'other' file!\n $_\n";
print OTHER $_;
}
}

-sln
 
J

Jürgen Exner

Steve said:
I have this chunk of code which is broken because I can't figure this
out. Basically I have a file handle, and the file it reads in always
starts wit ha number... 1-99. I want to ONLY get that number, and not
the rest of the line

That is trivial, just use the text in numerical context. The numerical
value of a text is the leading number in that text, e.g.

print "123 only the number but not this text will be printed" + 0;

If you "use warnings" (as you should) you will have to temporarily
disable them for this conversion.
because I want to be able to copy that line that
amount of times.

my $line = "32 and whatever other text";
print $line x ($line+0);

jue
 
S

Steve

I have this chunk of code which is broken because I can't figure this
out.  Basically I have a file handle, and the file it reads in always
starts wit ha number... 1-99.  I want to ONLY get that number, and not
the rest of the line because I want to be able to copy that line that
amount of times.

Where is the code that "copy that line that amount of times"?


while(<FH3>)
{
   my $x = 0;
#Make enough records based on quantity.. broken par

What does this mean?
   if(/^[0-9]|^[0-9][0-9]/)

This will only find the first digit.
   {
           print m/^[0-9]|^[0-9][0-9]/;

Don't need to do this again.
print "$1\n" if (/^(\d\d*)/)
#Check each line for a type

There must be a better way to do this.
Apparently each line could have multiple
items?

Untested:
        my $x = 0;
        while (/(SWITCH|MONITOR|ROUTER|KVM) |
                (LICENCES?) |
                (COMPUTER|LAPTOP|THIN) |
                (SOFTWARE) /xg
        ) {
            defined $1 && print HARDWARE $_;
            defined $2 && print LICENSES $_;
            defined $3 && print COMPUTER $_;
            defined $4 && print SOFTWARE $_;
            $x = 1;
        )


   if(/SWITCH|MONITOR|ROUTER|KVM/)
   {
           print HARDWARE $_;
           $x=1;
   }
   if(/LICENCE|LICENCES/)
   {
           print LICENSES $_;
           $x=1;
   }
   if(/COMPUTER|LAPTOP|THIN/)
   {
           print COMPUTER $_;
           $x=1;
   }
   if(/SOFTWARE/)
   {
           print SOFTWARE $_;
           $x=1;
   }
   if(/OS|'OPERATING SYSTEM'/)
   {
           print OS $_;
           $x=1;
   }
   if ($x == '0')
   {
           print "Warning, following line didn't match anything and was
appended to the 'other' file!\n $_\n";
           print OTHER $_;
   }
}

-sln

Sorry I'm no perl expert here, still learning. No idea what some of
the code you've changed even means. the \d\d* makes sense, except I
think it should be \d* right? the line will only ever start with 1 or
2 digits. Also, I just want to copy the entire line based on that
number. For example
3,ABC,SDFFDSF,344556

if that line is found, I want it to be duplicated 3 times, and chop
off the 15 after, so it will be:
ABC,SDFFDSF,344556
ABC,SDFFDSF,344556
ABC,SDFFDSF,344556
 
J

Jim Gibson

Steve said:
the \d\d* makes sense, except I
think it should be \d* right? the line will only ever start with 1 or
2 digits.

No. The equivalent to \d\d* would be \d+.

\d* means "zero or more digits"

\d+ means "one or more digits"

\d\d* would mean "one digit followed by zero or more digits", or, more
concisely, "one or more digits".
 
S

sln

No. The equivalent to \d\d* would be \d+.

\d* means "zero or more digits"

\d+ means "one or more digits"

\d\d* would mean "one digit followed by zero or more digits", or, more
concisely, "one or more digits".

Yes \d\d* ~ \d+ but I really meant \d\d? ~ \d{1,2} because I thought
he said "1 or 2" digits as a qualification.
So, my fault. I still don't quite trust the {n,m} qualifyer all the
way yet, the interaction with other qualifyers sometimes seems buggy,
especially when this form {,m} is in use.

-sln
 
S

sln

Sorry I'm no perl expert here, still learning. No idea what some of
the code you've changed even means.

You have to decide what it means, its just a different (condensed)
way of doing what your original code does.
the \d\d* makes sense, except I
think it should be \d* right? the line will only ever start with 1 or
2 digits. Also, I just want to copy the entire line based on that
number. For example
3,ABC,SDFFDSF,344556

if that line is found, I want it to be duplicated 3 times, and chop
off the 15 after, so it will be:

The line can be broken up without alterring the line.
Its all subjective.

use warnings;
use strict;

my $line = "3,ABC,SDFFDSF,344556";
my ($dupcnt, $data) = $line =~ /^(\d\d?),(.*)/;

print "Before -\n", ($line."\n") x $dupcnt;
print "After -\n", $data, "\n";

-sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top