LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-25-2017, 11:58 AM   #1
amateurscripter
Member
 
Registered: Nov 2011
Posts: 31

Rep: Reputation: Disabled
grep a scalar from an array in Perl


I've searched the web for solution but could not find it. I'm hoping you guys can suggest something.

I have the following data in a file:
...
...
XNYS|11/23/2017|Thanksgiving|A|
XNYS|11/24/2017|Day After Thanksgiving|P|218,219
XNYS|12/24/2017|Day before Christmas|P|218,219
XNYS|12/25/2017|Christmas|A|
XNYS|04/24/2017|Kemal Day|A|
XNYS|04242017|My Day1|A|
XNYS|04252017|Day 25|A|


I initialized the following in a perl script:

my $Today = `date +%m%d%Y`;
my $Holidayfile = '/tmp/perl/DoNotHolidaylist.txt';
my @verify;
my $fandler;


Then I open the file and then load its contents to an array. I then try to print out if there's a matching data.

open($fandler, "$Holidayfile") || die "[ERROR] Opening file $!";

my @list = <$fandler>;
close $fandler;

#@verify = grep /XNYS.*${Today}/, @list;
@verify = grep /XNYS/, @list;
@verify = grep /$(Today)/, @verify;
print "Today is a holiday: @verify\n";


I'm expecting the print out to be:
"Today is a holiday: XNYS|04252017|Day 25|A|"

However, regardless of what combo I tried with "{}" around "Today" or "()" around "Today" I just cannot get my expected line to be printed out.

If I replace the last line for @verify with the following, I do see what I expect so that leads me to believe, "$Today" is not be parsed as I expect it to be:

@verify = grep /04252017/, @verify;

Can someone help out?

Thanks
 
Old 04-25-2017, 12:09 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,103

Rep: Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145Reputation: 4145
https://perldoc.perl.org/functions/grep.html
based on this page you need to check the location of { } and/or ( )
 
Old 04-25-2017, 02:31 PM   #3
amateurscripter
Member
 
Registered: Nov 2011
Posts: 31

Original Poster
Rep: Reputation: Disabled
Thx Pan64 but did not get expected out put again when I updated

@verify = grep /XNYS/, @list;
@verify = grep /$Today/, @verify;

To:
@verify = grep (/XNYS/, @list);
@verify = grep(/$Today/, @verify);

Or to:
@verify = grep {/XNYS/, @list};
@verify = grep {/$Today/, @verify};

The output was:

Today is a holiday:


There was no value for "@verify"


Thx again.
 
Old 04-25-2017, 04:10 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
The date command outputs a new line so you are searching for "04252017\n"

Take a look at the perl command function chomp: http://perldoc.perl.org/functions/chomp.html

Last edited by norobro; 04-26-2017 at 01:16 PM. Reason: changed command to function
 
1 members found this post helpful.
Old 04-25-2017, 04:21 PM   #5
amateurscripter
Member
 
Registered: Nov 2011
Posts: 31

Original Poster
Rep: Reputation: Disabled
Thx NOROBRO!

That was a rookie mistake on my part. That did the trick. I gotta remember to remove the succeeding character. Thx again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl, push strings into array or scalar, not sure which, or how to do it. jmgibson1981 Programming 9 07-18-2016 02:35 AM
Matching File Names From Scalar Variable -- PERL 0.o Programming 5 03-29-2014 01:05 PM
[SOLVED] Perl: why $scalar += 0; catkin Programming 15 01-03-2011 10:19 AM
Perl - foreach loop reading only the last $scalar in a @list OldGaf Programming 3 08-08-2009 09:57 AM
[Perl] array in scalar noir911 Programming 2 12-10-2006 04:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration