×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Newby index function help

Newby index function help

Newby index function help

(OP)
I'm currently trying to learn PERL, and my instructor tasked me with the following and I'm kind of lost.

#!/usr/bin/perl
use strict;
use warnings;

# Grocery store inventory:
my $lines = <<'END_OF_REPORT';
0.95  300  White Peaches
1.45  120  California Avacados
5.50   10  Durian
0.40  700  Spartan Apples
END_OF_REPORT

my ($line1, $line2, $line3, $line4) = split "\n", $lines;
my ($cost, $quantity, $item) = split " ", $line1, 3;

my $a = index ($line1, "es" , 22 );
my $b = index ($line2, "es" , 28 );
my $c = index ($line3, "es" , 15 );
my $d = index ($line4, "es" , 23 );

print "$a\n";
print "$b\n";
print "$c\n";
print "$d\n";

The above shows

22
-1
-1
23

I'm supposed to search each line that is != 1 and print that line like such.  Without using prior knowledge that $a and $d will work.

print "Total value of $item on hand = \$", $cost * $quantity, "\n";

Please help.

RE: Newby index function help

Looks like you just need to be introduced to arrays and the foreach loop:

CODE


#!/usr/bin/perl
use strict;
use warnings;

# Grocery store inventory:
my $lines = <<'END_OF_REPORT';
0.95  300  White Peaches
1.45  120  California Avacados
5.50   10  Durian
0.40  700  Spartan Apples
END_OF_REPORT

my @lines = split "\n", $lines;

foreach my $line (@lines ) {
    my ($cost, $quantity, $item) = split " ", $line, 3;

    my $es_pos = index $line, "es";
    
    if ($es_pos > -1) {
        print "Total value of $item on hand = \$", $cost * $quantity, "\n";
    }
}

- Miller

RE: Newby index function help

(OP)
Thank you. That worked good.

What if one of the items was "escargo" ?

I would only want the items that end with "es" not anything with es in it.  Is there a way to do that ?

Here is what I got when I replaced Durian with escargo.

Total value of White Peaches on hand = $285
Total value of escargo on hand = $55
Total value of Spartan Apples on hand = $280
 

RE: Newby index function help

use a regex

CODE

   if ($line =~ /es$/) {
        print "Total value of $item on hand = \$", $cost * $quantity, "\n";
    }

or check that the index position of 'es' is at the length of the string -1 (as indexing starts at zero for perl)

CODE

    my $es_pos = index $line, "es";
    
    if ($es_pos == length($line)-1) {

 

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close