{"id":29261,"date":"2023-01-06T14:29:34","date_gmt":"2023-01-06T08:59:34","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/"},"modified":"2023-01-06T14:29:34","modified_gmt":"2023-01-06T08:59:34","slug":"solved-perl-programming","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/","title":{"rendered":"[Solved] Perl Programming"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-41244435\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"41244435\" data-parentid=\"41234882\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You&#8217;re talking about regular expressions and how to use them in Perl. Your question seems to be whether the answers you picked to homework are correct.<\/p>\n<p>The code you&#8217;ve added should do what you want, but it has syntax errors.<\/p>\n<pre><code>if ( ^x*$ ) {\n    print \"This is\";\n}\n<\/code><\/pre>\n<p>Your <em>pattern<\/em> is correct, but you don&#8217;t know how to use a regular expression in Perl. You&#8217;re missing the actual operator to tell Perl that you want a regular expression.<\/p>\n<p>The short form is this, where I&#8217;ve highlighted the important part with <code>#<\/code><\/p>\n<pre><code>if ( \/^x*$\/ ) {\n     #    #\n<\/code><\/pre>\n<p>The slashes <code>\/\/<\/code> tell Perl that it should match a pattern. The long form of it is:<\/p>\n<pre><code>if ( $_ =~ m\/^x*$\/ ) {\n     ## ## ##    #\n<\/code><\/pre>\n<p><code>$_<\/code> is the variable that you are matching against a pattern. The <code>=~<\/code> is the <em>matching operator<\/em>. The <code>m\/\/<\/code> constructs a pattern to match with. If you use <code>\/\/<\/code> you can leave out the <code>m<\/code>, but it&#8217;s clearer to put it in.<\/p>\n<p>The <code>$_<\/code> is called <em>topic<\/em>. It&#8217;s like a default variable that stuff goes into in Perl if you don&#8217;t specify another variable.<\/p>\n<pre><code>while ( &lt;$fh&gt; ) {\n    print $_ if $_ =~ m\/foo\/; # print all lines that contain foo\n}\n<\/code><\/pre>\n<p>This code can be written as <code>$_<\/code>, because a lot of commands in Perl assume that you mean <code>$_<\/code> when you don&#8217;t explicitly name a variable.<\/p>\n<pre><code>while ( &lt;$fh&gt; ) {    # puts each line in $_\n    print if m\/foo\/; # prints $_ if $_ contains foo\n}\n<\/code><\/pre>\n<p>You code looks like you wanted to do that, but in fact you have a <code>$row<\/code> in your loop. That&#8217;s good, because it is more explicit. That means it&#8217;s easier to read. So what you need to do for your match is:<\/p>\n<pre><code>while ( my $row = &lt;$fh&gt; ) {\n    if ( $row =~ m\/^x*$\/ ) {\n        print \"This is\";\n    }\n}\n<\/code><\/pre>\n<p>Now you will <em>iterate<\/em> each line of the file behind the <code>$fh<\/code> filehandle, and check if it matches the pattern <code>^x*$<\/code>. If it does, you print _&#8221;This is&#8221;. That doesn&#8217;t sound very useful.<\/p>\n<p>Consider this example, where I am using the <code>__DATA__<\/code> section instead of a file.<\/p>\n<pre><code>use strict;\nuse warnings;\n\nwhile ( my $row = &lt;DATA&gt; ) {\n    if ( $row =~ m\/^x*$\/ ) {\n        print \"This is\";\n    }\n}\n\n__DATA__\nfoo\nxxx\n\nx\nxxxxx\nbar\n<\/code><\/pre>\n<p>This will print:<\/p>\n<pre><code>This isThis isThis isThis is\n<\/code><\/pre>\n<p>It really does not seem to be very useful. It would make more sense to include the line that matched.<\/p>\n<pre><code>if ( $row =~ m\/^x*$\/ ) {\n    print \"match: $row\";\n}\n<\/code><\/pre>\n<p>Now we get this:<\/p>\n<pre><code>match: xxx\nmatch: \nmatch: x\nmatch: xxxxx\n<\/code><\/pre>\n<p>That&#8217;s almost what we expected. It matches a single <code>x<\/code>, and a bunch of <code>x<\/code>s. It did not match <code>foo<\/code> or <code>bar<\/code>. But it does match an empty line.<\/p>\n<p>That&#8217;s because you picked the wrong pattern. <\/p>\n<p>The <code>*<\/code> multiplier means  <em>match as many as possible, as least <strong>none<\/strong><\/em>.<br \/>\nThe <code>+<\/code> multiplier means <em>match as many as possible, at least <strong>one<\/strong><\/em>.<\/p>\n<p>So your pattern should be the one with <code>+<\/code>, or it will match if there is nothing, because <em>start of the line<\/em>, no <code>x<\/code>, <em>end of the line<\/em> matches an empty line.<\/p>\n<p>While you&#8217;re at it, you could also rename your variable. Unless you&#8217;re dealing with CSV, which has rows of data, you have <em>lines<\/em>, not <em>rows<\/em>. So <code>$line<\/code> would be a better name for your variable. Giving variables good, descriptive names is very important because it makes it easier to understand your program.<\/p>\n<pre><code>use strict;\nuse warnings;\n\nmy $filename=\"data.txt\";\n\nopen( my $fh, '&lt;:encoding(UTF-8)', $filename ) \n    or die \"Could not open file '$filename' $!\";\n\nwhile ( my $line = &lt;$fh&gt; ) {\n    if ( $line =~ m\/^x+$\/ ) {\n        print \"match: $line\";\n    }\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Perl Programming <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You&#8217;re talking about regular expressions and how to use them in Perl. Your question seems to be whether the answers you picked to homework are correct. The code you&#8217;ve added should do what you want, but it has syntax errors. if ( ^x*$ ) { print &#8220;This is&#8221;; } Your pattern is correct, but &#8230; <a title=\"[Solved] Perl Programming\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/\" aria-label=\"More on [Solved] Perl Programming\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[442],"class_list":["post-29261","post","type-post","status-publish","format-standard","hentry","category-solved","tag-perl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Perl Programming - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Perl Programming - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You&#8217;re talking about regular expressions and how to use them in Perl. Your question seems to be whether the answers you picked to homework are correct. The code you&#8217;ve added should do what you want, but it has syntax errors. if ( ^x*$ ) { print &quot;This is&quot;; } Your pattern is correct, but ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-06T08:59:34+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Perl Programming\",\"datePublished\":\"2023-01-06T08:59:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/\"},\"wordCount\":452,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"perl\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/\",\"name\":\"[Solved] Perl Programming - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-06T08:59:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-programming\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Perl Programming\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Perl Programming - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Perl Programming - JassWeb","og_description":"[ad_1] You&#8217;re talking about regular expressions and how to use them in Perl. Your question seems to be whether the answers you picked to homework are correct. The code you&#8217;ve added should do what you want, but it has syntax errors. if ( ^x*$ ) { print \"This is\"; } Your pattern is correct, but ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/","og_site_name":"JassWeb","article_published_time":"2023-01-06T08:59:34+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Perl Programming","datePublished":"2023-01-06T08:59:34+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/"},"wordCount":452,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["perl"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/","url":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/","name":"[Solved] Perl Programming - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-06T08:59:34+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-perl-programming\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-perl-programming\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Perl Programming"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=29261"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29261\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=29261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=29261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=29261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}